wait / notify和wait / interrupt有什么区别? [英] What is the difference between wait/notify and wait/interrupt?

查看:207
本文介绍了wait / notify和wait / interrupt有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

synchronized (Foo.class) {
    while (someCondition) {
        try {
            Foo.class.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();            
        }
    }
}

看来这个线程都是当其他线程在此线程上调用 interrupt() notify()时唤醒。两者之间是否有任何差异?

It seems that this thread both wakes when some other thread call interrupt() or notify() on this thread. Are there any differences between the two?

- 编辑 -

我知道一个用于通知对象,另一个中断线程。但是这两个导致了同样的后果,也就是说,这个线程被唤醒,所以我想问的是这两种情况的后果是如何相互不同的。

I know one is for notifying an object, the other interrupts a thread. But both of these lead to the same consequence, that is, this thread is waken up, so what I want to ask is how these 2 situations' consequences are different from each other.

推荐答案

当线程在某个监视器上调用notify时,它会唤醒正在该监视器上等待的单个线程,但哪个线程被唤醒由调度。 (或者一个线程可以调用notifyAll,它唤醒所有等待该监视器的线程,然后它们都争用监视器,然后输入回到等待。)这就是为什么呼叫的目标是不同的,通知是监视器,告诉调度程序选择要唤醒的线程。

When a thread calls notify on some monitor, it wakes up a single thread that's waiting on that monitor, but which thread gets woken is decided by the scheduler. (Alternatively a thread can call notifyAll, which wakes up all the threads waiting for that monitor, then they all contend for the monitor, then the losers go back to waiting.) That's why the target of the call is different, the notification is made to the monitor, which tells the scheduler to pick a thread to wake up.

与notify不同,中断以特定线程为目标。并且中断不要求被中断的线程在监视器上等待。对于要在监视器上调用wait的线程,它必须首先获取该监视器,然后等待监视直到线程完成等待或被中断的版本。

Unlike notify, interruption targets a specific thread. And interruption does not require that the interrupted thread be waiting on a monitor. For a thread to call wait on a monitor it has to have acquired that monitor first, then wait releases that monitor until the thread is done waiting or is interrupted.

Oracle的建议是仅使用中断取消。此外,java.util.concurrent中的类设计为使用中断进行取消。

Oracle's recommendation is to use interruption only for cancellation. Also the classes in java.util.concurrent are designed to use interrupt for cancellation.

在你的例子中,中断不会非常有效,因为控制不会离开while循环,线程仍然必须检查它正在等待的条件,并且没有检查while循环条件是否设置了中断标志。很可能被中断的线程会回到等待状态。

In your example interruption won't be very effective, because control doesn't leave the while loop, the thread still has to check the condition it's waiting on, and there's no check in the while loop condition for whether the interrupt flag is set. It's likely the thread that's interrupted will go right back to waiting.

为了让这个代码在被中断后退出,而不是返回等待,添加一个检查将标志状态中断到循环条件,并使catch块设置中断标志(在抛出异常时重置):

In order to make this code quit once it's interrupted, rather then return to waiting, add a check for the interrupted flag status to the loop condition, and have the catch block set the interrupt flag (which gets reset when the exception is thrown):

synchronized (Foo.class) {
    while (someCondition && !Thread.currentThread().isInterrupted()) {
        try {
            Foo.class.wait();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();            
        }
    }
}

这篇关于wait / notify和wait / interrupt有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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