gpt4 book ai didi

java - WAIT 和 BLOCKED 线程状态之间的区别

转载 作者:行者123 更新时间:2023-12-02 03:37:03 34 4
gpt4 key购买 nike

线程状态 WAIT 和线程状态 BLOCKED 有什么区别?

Thread.State documentation :

Blocked
A thread that is blocked waiting for a monitor lock is in this state.

Waiting
A thread that is waiting indefinitely for another thread to perform a particular action is in this state

没有向我解释其中的区别。

最佳答案

一旦线程在对象上调用wait(),就会进入等待状态。这称为等待状态。一旦线程达到等待状态,它就需要等待,直到其他线程调用对象上的 notify()notifyAll()

一旦该线程被通知,它将无法运行。可能其他线程也收到通知(使用 notifyAll()),或者第一个线程尚未完成其工作,因此它仍然处于阻塞状态,直到有机会为止。这称为阻塞状态。每当一个线程尝试获取对象上的锁并且其他线程已经持有该锁时,就会出现阻塞状态。

一旦其他线程离开并且该线程有机会,它就会进入可运行状态,之后它有资格根据 JVM 线程机制拾取工作并进入运行状态。

关于java - WAIT 和 BLOCKED 线程状态之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56854029/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com