简体   繁体   中英

How two threads can take lock on same object at a time?

I was reading Java Concurrency in Practice.

在此处输入图片说明

SO it is written that

When thread A executes a synchronized block, and subsequently thread B enters a synchronized block guarded by the same lock

How can two thread take lock on same object at a time? Thanks.

它们表示B在A释放后进入锁定状态

Each object has lock. Just before a thread enters the synchronized block to execute the code, it acquires the lock. When the thread has finished executing the block, it releases the lock. The second thread cannot acquire the lock during this time. It acquires the lock once it has been released by the first thread.

"When thread A executes a synchronized block, and subsequently thread B enters a synchronized block guarded by the same lock" .

There will only be one lock. Thread B can get hold of the lock only when Thread A releases the lock. " subsequently ". The author wants to say that the changes made by Thread A on the same object will be visible to Thread B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM