alcuni test su synchronized e singleton
This commit is contained in:
@@ -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
|
||||||
39
threads_test/test_2_singleton.java
Normal file
39
threads_test/test_2_singleton.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
22
threads_test/test_2_start.java
Normal file
22
threads_test/test_2_start.java
Normal 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");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
63
threads_test/test_3_start_l.java
Normal file
63
threads_test/test_3_start_l.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
63
threads_test/test_3_start_m.java
Normal file
63
threads_test/test_3_start_m.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
36
threads_test/test_3_static_synchronized_lock.java
Normal file
36
threads_test/test_3_static_synchronized_lock.java
Normal 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
28
threads_test/test_3_static_synchronized_methods.java
Normal file
28
threads_test/test_3_static_synchronized_methods.java
Normal 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user