假如你要调用某个对象的wait()方法,一般都要放在synchronized声明里面,why? 下面是答案。
Suppose d is the object we're using to invoke wait. When a thread invokes d.wait, it must own the intrinsic lock for d — otherwise an error is thrown. Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock.
When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened。
本文深入探讨了Java中对象wait()方法的使用原理。解释了为什么调用wait()必须在拥有对象锁的情况下进行,并且通常被放置在synchronized块内。此外,还介绍了线程如何通过释放锁并等待通知,以及如何通过notifyAll()方法唤醒等待的线程。
6222

被折叠的 条评论
为什么被折叠?



