简体   繁体   English

Java Thread wait()=>被阻止了吗?

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

According to Java thread state info calling wait() will result a thread to go in BLOCKED state. 根据Java线程状态信息调用wait()将导致线程进入BLOCKED状态。 However this piece of code will result (after being called) in a Thread in WAITING State. 但是,这段代码将在WAITING状态的Thread中生成(在被调用之后)。

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. 然后它变为BLOCKED尝试重新进入同步区域,直到所有其他线程都离开。

Relevant parts from the link you posted (about WAITING): 您发布的链接中的相关部分(关于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. 例如,在对象上调用Object.wait()的线程正在等待另一个线程在该对象上调用Object.notify()或Object.notifyAll()。

and (about BLOCKED): 和(关于BLOCKED):

A thread in the blocked state is waiting for a monitor lock to [...] reenter a synchronized block/method after calling Object.wait. 处于阻塞状态的线程正在等待监视器锁定,以便在调用Object.wait后重新进入同步块/方法。

The last part occurs when the thread tries to return from wait(), but not until then. 当线程尝试从wait()返回时,最后一部分发生,但直到那时才发生。

The monitor executes one thread at a time. 监视器一次执行一个线程。 Assuming you have T1-T10 threads, 9 are BLOCKED and one is RUNNABLE . 假设你有T1-T10线程,9个是BLOCKED ,一个是RUNNABLE Every once in a while, the monitor picks a new thread to run. 每隔一段时间,监视器就会选择一个新线程来运行。 When that happens, the chosen/current thread, say T1, goes from RUNNABLE to BLOCKED . 当发生这种情况时,所选择的/当前线程(例如T1)从RUNNABLE变为BLOCKED Then another thread, say, T2, goes from BLOCKED to RUNNABLE , becoming the current thread. 然后另一个线程,比如T2,从BLOCKED变为RUNNABLE ,成为当前线程。

When one of the threads needs some information to be made available by another thread, you use wait() . 当其中一个线程需要另一个线程提供某些信息时,可以使用wait() In that case, the thread will be flagged as WAITING until it is notify() ed. 在这种情况下,线程将被标记为WAITING直到它被notify() ed。 So, a thread that is waiting will not be executed by the monitor until then. 因此,在此之前,监视器不会执行正在等待的线程。 An example would be, wait until there are boxes to be unloaded. 一个例子是,等到有待卸载的盒子。 The guy loading boxes will notify me when that happens. 装箱的人会在发生这种情况时通知我。

In other words, both BLOCKED and WAITING are status of inactive threads, but a WAITING thread cannot be RUNNABLE without going to BLOCKED first. 换句话说, BLOCKEDWAITING都是非活动线程的状态,但是如果没有先进行BLOCKED ,则WAITING线程不能是RUNNABLE WAITING threads "don't want" to become active, whereas BLOCKED threads "want" to, but can't, because it isn't their turn. WAITING线程“不希望”变为活动状态,而BLOCKED线程“想要”但不能,因为它不是轮流。

I think. 我认为。

Where did you see it say stuff like that? 你在哪里看到它说的那样的东西?

In the same page you linked, thread.state , it clearly states that 在您链接的同一页面中, thread.state ,它清楚地说明了这一点

WAITING will be after Object.wait() 等待将在Object.wait()之后

BLOCKED will be before entering synchronized BLOCKED将在进入同步之前

There is some confusing terminology going on here. 这里有一些令人困惑的术语。 When a thread calls wait on an object it goes into the WAIT state. 当一个线程调用一个对象等待它进入WAIT状态。 When threads are waiting to grab a lock, they belong to the wait set for that lock, but they are in the BLOCKED state. 当线程等待获取锁时,它们属于该锁的等待集,但它们处于BLOCKED状态。

Confusing but somehow it makes sense! 令人困惑,但不知何故,这是有道理的!

Waiting is when it's not doing anything at all. 等待是什么时候什么也没做。 Blocked is when it's trying to start running again but hasn't been allowed to yet. 被阻止的是当它试图再次开始运行但尚未被允许时。

Just as a reminder, you should always call wait() inside a while loop waiting on the condition for entering the synchronized region/critical section. 提醒一下,你应该总是在while循环中调用wait(),等待进入同步区域/临界区的条件。 This is because Java has "spurious wakeups" (essentially, a thread can wakeup at any moment for no reason). 这是因为Java有“虚假的唤醒”(基本上,线程可以随时无故唤醒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM