Java Thread wait()=>阻塞? [英] Java Thread wait() => blocked?

查看:372
本文介绍了Java Thread wait()=>阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Java线程状态info 调用wait()会使线程进入BLOCKED状态。但是这段代码会在WAITING状态的线程中被调用。

 类bThread extends Thread {
public synchronized void run(){
try {
wait();
} catch(InterruptedException e){
e.printStackTrace();
}
}

}

有什么问题?任何人都可以向我解释这种行为吗?
任何帮助将不胜感激。

解决方案

线程正在等待,直到它被通知。



您发布的链接的相关部分(关于WAITING):

$ b例如,在对象上调用Object.wait()的线程正在等待另一个线程调用Object.notify()或Object.notifyAll()方法。


和(关于BLOCKED):


一个处于阻塞状态的线程正在等待一个监视器锁[...]在调用Object.wait之后重新输入一个同步的块/方法。


最后一个部分发生在线程尝试从wait()返回时,但是直到那时。


According to Java thread state info calling wait() will result a thread to go in BLOCKED state. However this piece of code will result (after being called) in a Thread in WAITING State.

class bThread extends Thread {
    public synchronized void run() {
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Have I got something wrong? Can anybody explain this behaviour to me? Any help would be appreciated!

解决方案

The thread is WAITING until it is notified. Then it becomes BLOCKED trying to reenter the synchronized region until all other threads have left.

Relevant parts from the link you posted (about WAITING):

For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object.

and (about BLOCKED):

A thread in the blocked state is waiting for a monitor lock to [...] reenter a synchronized block/method after calling Object.wait.

The last part occurs when the thread tries to return from wait(), but not until then.

这篇关于Java Thread wait()=>阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆