如果线程在同步块之前等待监视器释放或调用 wait() 有什么区别吗

发布于 2024-09-26 11:47:01 字数 160 浏览 0 评论 0原文

我读过很多关于线程状态的文档,其中一些告诉我们有两种不同的状态:阻塞(同步之前)和等待(如果调用等待),其他一些则告诉我们只有一种状态:等待。此外,一些文档告诉您应该为每个 wait() 调用 notification() ,如果您不这样做,那么即使监视器已解锁, wait() 线程也将永远没有资格执行。

I've read many docs about thread states, some of them tells that there is two different states: blocked (before synchronized) and wait (if calls wait), some others are telling that there is only one state: wait. Moreover, some docs telling that you should call notify() for every wait() and if you don't then threads waiting() will never be eligible for execution even if monitor is unlocked.

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

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

发布评论

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

评论(4

恬淡成诗 2024-10-03 11:47:01

从你的最后一句话中我发现你没有完全理解 synchronizedwait()/notify() 之间的区别。

基本上,监视器有锁定条件。这几乎是正交的概念。

  • 当线程进入synchronized块时,它会获取锁。当线程离开该块时,它会释放锁。只有一个线程可以拥有特定监视器上的锁。

  • 当拥有锁的线程调用 wait() 时,它会释放锁并开始等待其条件。当拥有锁的线程调用 notify() 时,等待条件的线程之一(在 notifyAll() 情况下为所有线程)将有资格执行(并开始等待获取锁,因为通知线程仍然拥有锁)。

因此,等待获取锁 (Thread.State.BLOCKED) 和等待监视器的条件 (Thread.State.WAITING) 是不同且独立的状态。

如果您查看 ,这种行为会变得更加清晰Lock 类 - 它实现与 synchronized 块相同的同步原语(带有一些扩展),但提供了锁和条件之间的明确区别。

From you last sentence I see you don't fully understand the difference between synchronized and wait()/notify().

Basically, monitor has lock and condition. It's almost orthogonal concepts.

  • When thread enters a synchronized block, it acquires a lock. When thread leaves that block, it releases a lock. Only one thread can have a lock on a particular monitor.

  • When thread having a lock calls wait(), it releases a lock and starts waiting on its condition. When thread having a lock calls notify(), one of the threads (all threads in the case of notifyAll()) waiting on the condition becomes eligible for execution (and starts waiting to acquire a lock, since notifying thread still has it).

So, waiting to acquire a lock (Thread.State.BLOCKED) and waiting on the monitor's condition (Thread.State.WAITING) are different and independent states.

This behaviour becames more clear if you look at Lock class - it implements the same synchronization primitives as synchronized block (with some extensions), but provides clear distinction between locks and conditions.

凹づ凸ル 2024-10-03 11:47:01

两种不同的状态 BLOCKED 和等待。

如果没有人通知(或打断)您,则永远等待的部分是正确的。

There are two different states BLOCKED and WAITING.

The part about waiting forever if no one notifies (or interrupts) you is true.

只涨不跌 2024-10-03 11:47:01

标准文档在这里

当线程调用Object.wait
方法,它释放这个获取的
监视并放入 WAITING (或
TIMED_WAITING 如果我们调用超时
等待方法的版本)状态。
现在当线程收到通知时
通过 notify() 或通过 notifyAll() 调用
相同的对象然后等待状态
线头和线头
开始尝试重新获得所有
它在
等待呼叫的时间。有一段时间那里
可能有几个线程试图
重新获得(或者可能第一次获得
时间)他们的显示器。如果超过一个
线程尝试获取监视器
一个特定的对象,那么只有一个
线程(由 JVM 调度程序选择)
被授予监视器和所有其他
线程被置于BLOCKED状态。

Standard doc is here

When a thread calls Object.wait
method, it releases this acquired
monitor and is put into WAITING (or
TIMED_WAITING if we call the timeout
versions of the wait method) state.
Now when the thread is notified either
by notify() or by notifyAll() call on
the same object then the waiting state
of the thread ends and the thread
starts attempting to regain all the
monitors which it had acquired at the
time of wait call. At one time there
may be several threads trying to
regain (or maybe gain for the first
time) their monitors. If more than one
threads attempt to acquire the monitor
of a particular object then only one
thread (selected by the JVM scheduler)
is granted the monitor and all other
threads are put into BLOCKED state.

风吹雪碎 2024-10-03 11:47:01

从Java的角度来看(Thread.State ),有两种不同的状态: BLOCKED 和 WAITING 。当线程在对象上同步时,它处于 BLOCKED 状态。线程执行wait后,就处于WAITING状态。

在Linux平台上,Java线程是操作系统本机线程。 BLOCKED 和 WAITING 状态的操作系统线程状态都是可中断睡眠。当使用 ps 检查时,BLOCKED 和 WAITING 线程的状态都是“Sl+”。

In Java's perspective (Thread.State), there are two different states: BLOCKED and WAITING . When a thread synchronizes on a Object, it is in BLOCKED state. After a thread executes wait, it is in WAITING state.

On Linux platform, Java thread is OS native thread. The OS thread state for both BLOCKED and WAITING states is Interruptible sleep. When being checked with ps, the state for both BLOCKED and WAITING threads is "Sl+".

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