【问题标题】:Spurious wakeups on windows. Is it possible?窗户上的虚假唤醒。是否可以?
【发布时间】:2014-04-25 14:43:09
【问题描述】:

我最近学习了“虚假唤醒” 有人说这个问题只可能出现在某些类型的 Linux PC 上。

我使用窗户。

我为虚假唤醒编写了测试。我得到的结果是可能的。但我想为你展示这个测试。也许我在某个地方犯了错误。

我的初始变体:

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TestSpuriousWakeups {
    static final int MAX_THREADS = 600;

    static final Object mutex = new Object();

    static final CountDownLatch allThreadsStarted =
            new CountDownLatch(MAX_THREADS);
    static final CountDownLatch allThreadsFinished =
            new CountDownLatch(1);

    static /*final*/ volatile AtomicInteger processedThreads = new AtomicInteger();
    static /*final*/ volatile AtomicInteger notifiedThreads = new AtomicInteger();

    final  int n  = 10;

    static volatile boolean continueCondition = true;

    static final Random sleepRandom = new Random();

    static class Worker extends Thread {
        public void run() {
            try {
                synchronized (mutex) {
                    allThreadsStarted.countDown();

                    mutex.wait();
                }

                continueCondition = true;
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                processedThreads.incrementAndGet();
            }
        }
    }

    static class Notifier extends Thread {
        public void run() {
            while (true) {

                if (processedThreads.get() == MAX_THREADS)
                    break;

                synchronized (mutex) {
                    doStuff();

                    mutex.notify();
                    continueCondition = false;
                    notifiedThreads.incrementAndGet();
                }
            }

            allThreadsFinished.countDown();
        }

        // just to emulate some activity
        void doStuff() {
            try { Thread.sleep(sleepRandom.nextInt(5)); }
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < MAX_THREADS; i++)
            new Worker().start();

        // wait for all workers to start execution
        allThreadsStarted.await();

        new Notifier().start();

        // wait for all workers and notifier to finish execution
        allThreadsFinished.await();

        System.out.println("Spurious wakeups count: "
                + (MAX_THREADS - notifiedThreads.get()));
    }
}

4次随机执行:

Spurious wakeups count: -20
Spurious wakeups count: -5
Spurious wakeups count: 0
Spurious wakeups count: -407

我想知道不同的价值观。

我添加了一对行来运行方法:

static class Notifier extends Thread {
        public void run() {
            while (true) {

                while (!continueCondition)  //added string
                    doStuff();              //added string            

                // all threads finished their execution
                if (processedThreads.get() == MAX_THREADS)
                    break;

                synchronized (mutex) {
                    doStuff();

                    mutex.notify();
                    continueCondition = false;
                    notifiedThreads.incrementAndGet();
                }
            }

            allThreadsFinished.countDown();
        }

在它之后我无法得到其他东西

Spurious wakeups count: 0

这真的是我的实验中的虚假唤醒或错误吗?

附言

我注意到我看到了负数。因此显然这是实验错误。但我不明白原因。

【问题讨论】:

    标签: java concurrency notifications synchronization wait


    【解决方案1】:

    两件事

    1. 虚假唤醒是真实的,即使在 Windows 上也是如此。这记录在 WinAPI 中:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682052(v=vs.85).aspx
    2. 您的测试中有竞争条件。所以,我认为它不太准确。

    竞争发生在工作线程中同步块的退出和它们到达处理线程.incrementAndGet() 之间。通知程序将在此期间旋转,通知可能获得或未获得锁的线程。

    换句话说

    1. 在工作线程获取互斥锁之前,Notifier 可能会旋转两次(即 notify() 两次)。
    2. 在最后一个线程退出同步块但尚未到达其 finally 块后,通知程序可能会自旋。

    您添加的两行更改了输出,因为通过减慢通知程序的速度,您掩盖了比赛。 (通过给 Worker 大量时间进入互斥体。)

    希望这有点道理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      相关资源
      最近更新 更多