The explanation in the book is very simple and clear, but it may be a little difficult to use.
The synchronization object of synchronized method refers to the class instance object,
The synchronization object of synchronized static method refers to the class itself, that is, all objects share the synchronization object.
The problem lies in
Public int kk (){
G ();
}
Which object is used when G () is called in KK? Yes. The instance object is used! Whether you use g () or even. G (), the instance object takes precedence.
To use a class object, you must call even. G () outside the even class ()
Class generator {
Private Static int even = 0;
Random r = new random (1000 );
Generator (){
}
Public synchronized int F (){
Even ++;
Even ++;
Return even;
}
Public synchronized static int g (){
Even ++;
Try {
Thread. Sleep (10 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Even ++;
Return even;
}
Public int kk (){
G ();
}
}