本文介绍了Java中的虚假唤醒真的发生了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到各种与锁相关的问题,并且(几乎)总是找到'因为虚假的唤醒而导致的循环'术语我想,有没有人经历过这样的唤醒(假设一个像样的硬件/软件环境)例如)?

Seeing various locking related question and (almost) always finding the 'loop because of spurious wakeups' terms I wonder, has anyone experienced such kind of a wakeup (assuming a decent hardware/software environment for example)?

我知道虚假一词意味着没有明显的理由,但这种事件可能是什么原因?

I know the term 'spurious' means no apparent reason but what can be the reasons for such kind of an event?

(注意:我不是在质疑循环练习。)

( Note: I'm not questioning the looping practice.)

编辑:一个帮助问题(对于那些喜欢代码示例的人):

A helper question (for those who like code samples):

如果我有以下程序,我运行它:

If I have the following program, and I run it:

public class Spurious {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition cond = lock.newCondition();
        lock.lock();
        try {
            try {
                cond.await();
                System.out.println("Spurious wakeup!");
            } catch (InterruptedException ex) {
                System.out.println("Just a regular interrupt.");
            }
        } finally {
            lock.unlock();
        }
    }
}

我该怎么做才能唤醒这个等待虚假而不等待永远的随机事件?

What can I do to wake this await up spuriously without waiting forever for a random event?

推荐答案

维基百科有这样的消息:

The Wikipedia article on spurious wakeups has this tidbit:

摘要:如果Linux进程发出信号则表示等待线程将各自享受一个漂亮的,热的虚假的唤醒

Summary: If a Linux process is signaled its waiting threads will each enjoy a nice, hot spurious wakeup.

我买它。这是一个比通常模糊的性能原因更容易吞下的药丸。

I buy it. That's an easier pill to swallow than the typically vague "it's for performance" reason often given.

这篇关于Java中的虚假唤醒真的发生了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:46