Synchronization method block

Synchronization method block

The above two methods are relatively rigid. Normal method synchronization uses the current object as the lock, and static method synchronization uses the current class object as the lock.

So introduce a more flexible way: synchronize the block, pass the lock object as a parameter

synchronized(account){} The synchronization monitor can prevent multiple threads from concurrently accessing the same shared resource. Only one thread can obtain the lock on the synchronization monitor at any time. When the synchronization code block is executed, the thread will release the heap The lock of the synchronization monitor.


Guess you like

Origin blog.csdn.net/qq_45874107/article/details/113876446