简体   繁体   English

Java并发编程

[英]Concurrent Programming in Java

Is the lock retained by the thread when context switch occur. 发生上下文切换时,线程是否保留锁定。 When we invoke a wait() the locks are given up by thread, what happens when theres a context switch. 当我们调用wait()时,锁由线程放弃,当上下文切换时会发生什么。

Thanks 谢谢

Yes, locks are retained during a context switch. 是的,在上下文切换期间会保留锁。 In fact, that's the whole point of a lock. 实际上,这就是锁定的全部要点。

When a thread calls wait it relinquishes the lock. 当线程调用wait它将放弃锁。 When wait returns the lock is re-obtained, so you can guarantee that when control returns to your thread it holds all the locks you expect. 等待返回时,将重新获得该锁,因此可以保证当控制权返回到线程时,该锁将保留您期望的所有锁。

The thread that invoked wait() gets suspended until some other thread invokes notify*() on the object. 调用wait()的线程将被挂起,直到其他线程在对象上调用notify*()为止。 At that point, when another context switch occurs, the wait() call will return and the waiting thread will immediately try to re-acquire the lock before proceeding further. 到那时,当另一个上下文切换发生时, wait()调用将返回,并且等待线程将立即尝试重新获取该锁,然后再继续操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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