9-12-16
Public class testmain {
Public static void main (string ARGs []) {
Testsynchronized R1 = new testsynchronized ();
Testsynchronized r2 = new testsynchronized ();
Thread T1 = new thread (R1, "T1 ");
Thread t2 = new thread (R1, "T2 ");
T1.start ();
T2.start ();
}
}
Class testsynchronized implements runnable
{
Private Int J = 0;
Public void run ()
{
For (INT I = 0; I <3; I ++)
{
Single. getinstance (). printth ();
}
}
}
Class Single {
Private Static Single Instance;
Private;
Private single (){
This. A = new ();
System. Out. println ("called new instance a constructor mathod ");
}
Public static single getinstance (){
If (instance = NULL ){
Instance = new single ();
System. Out. println ("called new instance single constructor mathod ");
}
/* Instance = new single ();
System. Out. println ("called new instance single constructor mathod ");*/
Return instance;
}
Public synchronized void printsomething () {// If the synchronized keyword is not added. The I value is irregular.
Int I = A. Geti ();
I ++;
A. SETI (I );
System. out. println (thread. currentthread (). getname () + ": A Reference is" +. tostring () + "and. is is "+. geti ());
}
}
Class {
Private int I;
Public int Geti (){
Return I;
}
Public void SETI (int I ){
This. I = I;
}
}
-------------------
The following result uses the synchronization keyword.
Called new instance a constructor Mathod
Called new instance single constructor Mathod
T2: A Reference is test. A @ 10b30a7and A. Is is1
T2: A Reference is test. A @ 10b30a7and A. Is is2
Called new instance a constructor Mathod
Called new instance single constructor Mathod
T1: A Reference is test. A @ 1a758cband A. Is is1
T1: A Reference is test. A @ 1a758cband A. Is is2
T1: A Reference is test. A @ 1a758cband A. Is is3
T2: A Reference is test. A @ 10b30a7and A. Is is3
-------
The following result indicates that no synchronization keyword is used.
"T1: A is a @ d9f9c3 and A. I is 1
T2: A is a @ d9f9c3 and A. I is 2
T1: A is a @ d9f9c3 and A. I is 3
T2: A is a @ d9f9c3 and A. I is 4
T1: A is a @ d9f9c3 and A. I is 5
T2: A is a @ d9f9c3 and A. I is 6"
Each single object has an instance A, and the multi-thread t1 and t2 access the instance under that single state, which is not necessarily.
------
T2 called new instance a constructor mathod and A is a @ d9f9c3
T1 called new instance a constructor mathod and A is a @ 757aef
T2 called new instance single constructor mathod and single is single @ 9cab16
T1 called new instance single constructor mathod and single is single @ 1a46e30
T2: A reference is a @ 757 aefand A. Is is1
T1: A reference is a @ 757 aefand A. Is is2
T2: A reference is a @ 757 aefand A. Is is3
T1: A reference is a @ 757 aefand A. Is is4
T2: A reference is a @ 757 aefand A. Is is5
T1: A reference is a @ 757 aefand A. Is is6
As can be seen from the above program, a @ d9f9c3 is under single @ 9cab16, while a @ 757aef is under single @ 1a46e30.
However, T2 and T1 do not select a @ 757aef object. As you can imagine, two CPUs (t1 and t2), two instances of object A in the memory, and two memory blocks.
Which memory block is not necessarily selected for the two CPUs. Random. However, if you want to output the I value in object a in a linear order, you need to add the synchronization keyword.