java多线程中Object.wait()和Condition.await()是否会释放当前线程锁占有的锁

发布于 2022-08-30 01:01:55 字数 1526 浏览 32 评论 0

这个问题源自我问我们技术经理一个多线程问题时,他的回答让我迷惑。

我刚开始深入研究多线程,一直认为Object.wait()/Condition.await()让当前线程阻塞的同时,也会释放当前线程对该condition对象的锁。在之前的一些测试代码中也显示wait后,线程上的锁被释放了。但是我们经理却坚持当前线程会占用锁。

查看Object.wait()API 描述如下:

    Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). 

    The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. 

其中“the thread releases ownership of this monitor” 说道当前线程会释放这个对象监控器的所有权。

问题一:这里的monitor怎么理解?监视器(monitor)和锁是什么关系?
这个monitor就是一个术语,或者是一个特殊的类(只包含私有域),但是在java中并没有严格遵守这个概念。个人认为可以简单的理解为锁。

同样的,Condition.await()方法中也有类似描述。

    The lock associated with this Condition is atomically released and the current thread becomes disabled for thread scheduling purposes...

也说道,调用await()有,这个条件对象关联的锁被“原子级地”释放。。。

问题二:这个原子级的释放是什么意思?
“原子级”其实就是为了保证一个操作的完整性,原子级的释放保证了一个原子操作不会因为线程的突然挂起或者说阻塞而破坏这次操作。

这都能说明调用wait()或者await()后,当前线程会释放该条件对象关联的锁吧?!但是我们经理说不会释放又是什么意思?是我的理解太浅显么?还是我们经理错了?

希望多线程的资深前辈能不吝赐教!不胜感激

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2022-09-06 01:01:55

你的经理错了 或者你们的沟通有问题吧. 

两个线程用object1wait/notify, 是这样:

thread1 得到object1 的 monitor, 调用 object1.wait() 
  - 释放object1 的 monitor, thread1 wait;

thread2 得到 object1 的 monitor, 调用 object1.notify() 
  - 激活thread1, 释放object1 的 monitor;

thread1 得到 object1 的 monitor, 从object1.wait()返回, thread1接着执行.

关于monitor, 这个是多进程/线程同步的 一个术语, 见:
Operating Systems Design and Implementation, Third Edition
section 2.2

A monitor is a collection of procedures, variables, and data
structures that are all grouped together in a special kind of module
or package. Processes may call the procedures in a monitor whenever
they want to, but they cannot directly access the monitor's internal
data structures from procedures declared outside the monitor. This
rule, which is common in modern object-oriented languages such as
Java, was relatively unusual for its time, although objects can be
traced back to Simula 67.

东风软 2022-09-06 01:01:55

In all cases, before this method can return the current thread must

  • re-acquire the lock associated with this condition. When the

  • thread returns it is guaranteed to hold this lock.

    会释放,其他线程执行Condition.signal(),之前的线程会重新获得锁,继续执行,

    AbstractQueuedSynchronizer.java 第2040行,释放锁

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文