alcuni test su synchronized e singleton

This commit is contained in:
2014-10-12 16:07:22 +02:00
parent dfa68cf46d
commit 63f94029df
7 changed files with 254 additions and 1 deletions

View File

@@ -1,7 +1,9 @@
package threads_test;
/** /**
* Created by Giovanni on 04/10/2014. * Created by Giovanni on 04/10/2014.
*/ */
public class start { public class test_1_start {
public static class xthread implements Runnable{ public static class xthread implements Runnable{
@Override @Override

View File

@@ -0,0 +1,39 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_2_singleton {
private test_2_singleton(){
a="initialized";
}
private String a;
//////////////////////////////////////////////////////////////
private static class inner_test_2_singleton {
private static final test_2_singleton x = new test_2_singleton() ;
}
public static test_2_singleton get_instance(){
return inner_test_2_singleton.x;
}
}

View File

@@ -0,0 +1,22 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_2_start {
public static void main (String[] args) {
test_2_singleton o= test_2_singleton.get_instance();
test_2_singleton o2= test_2_singleton.get_instance();
if (o == o2 )
System.out.print("Equal");
}
}

View File

@@ -0,0 +1,63 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_3_start_l {
public static void main (String[] args) {
class Thread1 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_lock.op1();
}
}
class Thread2 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_lock.op2();
}
}
class Thread3 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_lock.op3();
}
}
Thread a = new Thread(new Thread1());
a.start();
Thread b = new Thread(new Thread2());
b.start();
Thread c = new Thread(new Thread3());
c.start();
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
b.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
c.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,63 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_3_start_m {
public static void main (String[] args) {
class Thread1 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_methods.op1();
}
}
class Thread2 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_methods.op2();
}
}
class Thread3 implements Runnable{
@Override
public void run() {
test_3_static_synchronized_methods.op3();
}
}
Thread a = new Thread(new Thread1());
a.start();
Thread b = new Thread(new Thread2());
b.start();
Thread c = new Thread(new Thread3());
c.start();
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
b.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
c.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,36 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_3_static_synchronized_lock {
public static void op1() {
try {
synchronized (test_3_static_synchronized_lock.class) {
Thread.currentThread().sleep(5000);
System.out.print("OP1 eseguita");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void op2() {
synchronized (test_3_static_synchronized_lock.class) {
System.out.print("OP2 eseguita");
}
}
public static void op3() {
System.out.print("OP3 eseguita");
}
}

View File

@@ -0,0 +1,28 @@
package threads_test;
/**
* Created by Giovanni on 12/10/2014.
*/
public class test_3_static_synchronized_methods {
public static synchronized void op1() {
try {
Thread.currentThread().sleep(5000);
System.out.print("OP1 eseguita");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static synchronized void op2() {
System.out.print("OP2 eseguita");
}
public static void op3() {
System.out.print("OP3 eseguita");
}
}