Synchronized
1. Multi-threaded execution of the same object synchronized function, thread synchronization is correct;
The instance code is as follows:
public class Test1 implements runnable{
Defining the same object
Static TEST2 action;
public static void Main (string[] args) {
//TODO auto-generated method Stub
class cls;
try {
cls = Class.forName ("Test2");
action = (Test2) cls.newinstance ();
test1 t1 = new Test1 ();
for (int i=0;i<1;i++) {
new Thread (T1). Start ();
new Thread (T1). Start ();
new Thread (T1). Start ();
new Thread (T1). Start ();
}
} catch (Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
public void Run () {
TODO auto-generated Method Stub
try {
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public class Test2 {
public static void Main (string[] args) {
}
Call the synchronized function of the same object without adding STATCI adornments
Public synchronized void Getrandomstring () throws Interruptedexception {
System.out.println ("$$$$$$$$$$$$$$$$$$");
Thread.Sleep (1000);
Random random = new random ();
int aa = Random.nextint (10);
int bb = Random.nextint (10);
int cc = Random.nextint (10);
System.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
System.out.println ("############");
}
}
=====================================================
=====================================================
2. Multi-threaded execution of different object synchronized function, thread synchronization error;
The instance code is as follows:
public class Test1 implements runnable{
public static void Main (string[] args) {
try {
test1 t1 = new Test1 ();
for (int i=0;i<100;i++) {
new Thread (T1). Start ();
new Thread (T1). Start ();
new Thread (T1). Start ();
new Thread (T1). Start ();
}
} catch (Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
public void Run () {
TODO auto-generated Method Stub
Class CLS;
try {
CLS = Class.forName ("Test2");
Test2 action = (Test2) cls.newinstance ();
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public class Test2 {
public static void Main (string[] args) {
}
/**
* @param args
* @throws interruptedexception
*/
// Call the synchronized function of different object, need to add statci adornment, otherwise thread synchronization fails
public synchronized void getrandomstring () throws interruptedexception {
system.out.println ("$$$$$$$$$$$$$$$$$$");
thread.sleep (1000);
random random = new Random ();
int aa = Random.nextint (10);
int bb = random.nextint (10);
int cc = Random.nextint (10);
system.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
system.out.println ("############");
}
}
====================================================
============================================== =======
3. Multithreaded execution of synchronized static functions of different objects, thread synchronization is correct;
The instance code is as follows:
public class Test1 implements runnable{
public static void Main (string[] args) {
try {
Test1 T1 = new Test1 ();
for (int i=0;i<100;i++) {
New Thread (T1). Start ();
New Thread (T1). Start ();
New Thread (T1). Start ();
New Thread (T1). Start ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Run () {
TODO auto-generated Method Stub
Class CLS;
try {
CLS = Class.forName ("Test2");
Test2 action = (Test2) cls.newinstance ();
Action.getrandomstring ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public class Test2 {
public static void Main (string[] args) {
}
/**
* @param args
* @throws interruptedexception
*/
Call the synchronized function of different object, need to add statci adornment, otherwise thread synchronization fails
Public synchronized static void Getrandomstring () throws Interruptedexception {
System.out.println ("$$$$$$$$$$$$$$$$$$");
Thread.Sleep (1000);
Random random = new random ();
int aa = Random.nextint (10);
int bb = Random.nextint (10);
int cc = Random.nextint (10);
System.out.println (string.valueof (aa) +string.valueof (BB) +string.valueof (cc));
System.out.println ("############");
}
}
=========================================================
==========================================================
Synchronized: Summary of thread synchronization method usage