When a method is decorated with synchronized, it indicates that the method locks the current object.
The effect of adding synchronized to the method and modifying the object with synchronized is consistent.
A thread can have more than one lock tag, and an object will only give monitor to a thread at most.
Synchronized is at the expense of the efficiency at which the program runs, so the scope of the mutex code block should be controlled as much as possible.
The Synchronized property of the method itself is not inherited and can only be overridden.
The blocking that occurs because the thread did not get the lock tag is different from the block in the previous five basic states, called the lock pool.
Each object has its own space for a lock pool that is used to place the thread waiting to run.
Which thread in these threads gets the lock tag is determined by the system.
1 Package tomtexts;2 3 Public classtomtexts_22 {4 Public Static voidMain (String args[])5 {6 intI,a;7 Try{//Monitor a block of code8I=0;9A= the/i;Ten return; One } A Catch(arithmeticexception e) -{//capturing an exception that is removed by 0 -System. out. println ("be removed by 0"); the } - } - -}
A thread can have more than one lock tag, and an object will only give monitor to a thread at most