Java Thread Series (v) synchronizedsynchronized lock re-entry
The keyword synchronized has the ability to re-enter the lock, that is, when using synchronized, when the thread waits for the lock on an object, it can again get the lock on the object when it is requested again. Release the lock when an exception occurs.
Synchronized exception synchronized code block
The use of the synchronized declaration method in some cases is a disadvantage, such as a thread call synchronous method to perform a long time task, then the B thread must wait for a long time to execute, in which case can use the synchronized code block to optimize the code execution time , which is usually said to reduce the granularity of the lock.
Synchronized code block case "Com.herolei._1_2sync. Objectlock "
Synchronized to Chang lock problem "com.herolei._1_2sync. Stringlock "
Note: The lock object changes the problem, when using an object to lock the operation, pay attention to the object itself changes, then hold the lock is different.
Record a little bit every day. Content may not be important, but habits are important!
Java Thread Series (v) synchronized