8

当我偶然发现一些我不理解的行为时,我试图使用 Java 同步“原语”(synchronized、wait()、notify() )来实现类似于 Java 的有界BlockingQueue接口的东西。

我创建了一个能够存储 1 个元素的队列,创建了两个等待从队列中获取值的线程,启动它们,然后尝试将两个值放入主线程的同步块中的队列中。大多数情况下它可以工作,但有时等待一个值的两个线程似乎开始互相唤醒并且不让主线程进入同步块。

这是我的(简化的)代码:

import java.util.LinkedList;
import java.util.Queue;

public class LivelockDemo {
    private static final int MANY_RUNS = 10000;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < MANY_RUNS; i++) { // to increase the probability
            final MyBoundedBlockingQueue ctr = new MyBoundedBlockingQueue(1);

            Thread t1 = createObserver(ctr, i + ":1");
            Thread t2 = createObserver(ctr, i + ":2");

            t1.start();
            t2.start();

            System.out.println(i + ":0 ready to enter synchronized block");
            synchronized (ctr) {
                System.out.println(i + ":0 entered synchronized block");
                ctr.addWhenHasSpace("hello");
                ctr.addWhenHasSpace("world");
            }

            t1.join();
            t2.join();

            System.out.println();
        }
    }

    public static class MyBoundedBlockingQueue {
        private Queue<Object> lst = new LinkedList<Object>();;

        private int limit;

        private MyBoundedBlockingQueue(int limit) {
            this.limit = limit;
        }

        public synchronized void addWhenHasSpace(Object obj) throws InterruptedException {
            boolean printed = false;
            while (lst.size() >= limit) {
                printed = __heartbeat(':', printed);
                notify();
                wait();
            }
            lst.offer(obj);
            notify();
        }

        // waits until something has been set and then returns it
        public synchronized Object getWhenNotEmpty() throws InterruptedException {
            boolean printed = false;
            while (lst.isEmpty()) {
                printed = __heartbeat('.', printed); // show progress
                notify();
                wait();
            }
            Object result = lst.poll();
            notify();
            return result;
        }

        // just to show progress of waiting threads in a reasonable manner
        private static boolean __heartbeat(char c, boolean printed) {
            long now = System.currentTimeMillis();
            if (now % 1000 == 0) {
                System.out.print(c);
                printed = true;
            } else if (printed) {
                System.out.println();
                printed = false;
            }
            return printed;
        }
    }

    private static Thread createObserver(final MyBoundedBlockingQueue ctr,
            final String name) {
        return new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(name + ": saw " + ctr.getWhenNotEmpty());
                } catch (InterruptedException e) {
                    e.printStackTrace(System.err);
                }
            }
        }, name);
    }
}

这是我在“阻塞”时看到的内容:

(skipped a lot)

85:0 ready to enter synchronized block
85:0 entered synchronized block
85:2: saw hello
85:1: saw world

86:0 ready to enter synchronized block
86:0 entered synchronized block
86:2: saw hello
86:1: saw world

87:0 ready to enter synchronized block
............................................

..........................................................................

..................................................................................
(goes "forever")

但是,如果我将 addWhenHasSpace 和 getWhenNotEmpty 方法的 while(...) 循环内的 notify() 调用更改为 notifyAll(),它“总是”通过。

我的问题是:为什么在这种情况下 notify() 和 notifyAll() 方法的行为会有所不同,以及 notify() 的行为为什么会这样?

在这种情况下,我希望这两种方法的行为方式相同(两个线程等待,一个阻塞),因为:

  1. 在我看来,在这种情况下 notifyAll() 只会唤醒另一个线程,与 notify() 相同;
  2. 看起来唤醒线程的方法的选择会影响被唤醒的线程(我猜想变成 RUNNABLE)和主线程(已经被 BLOCKED)后来竞争锁的方式——这不是我所期望的javadoc 以及在互联网上搜索该主题。

或者也许我完全做错了什么?

4

2 回答 2

2

使用内在锁定似乎存在某种公平/闯入 - 可能是由于一些优化。我猜,本机代码检查当前线程是否已通知监视器它即将等待并允许它获胜。

替换为synchronizedReentrantLock它应该可以按您的预期工作。这里不同的是如何ReentrantLock处理它通知的锁的服务员。


更新:

有趣的发现在这里。你看到的是main线程进入之间的竞赛

        synchronized (ctr) {
            System.out.println(i + ":0 entered synchronized block");
            ctr.addWhenHasSpace("hello");
            ctr.addWhenHasSpace("world");
        }

而另外两个线程进入各自的synchronized区域。如果主线程在两者中的至少一个之前没有进入其同步区域,您将体验到您所描述的这种活锁输出。

似乎正在发生的事情是,如果两个消费者线程都首先命中同步块,它们将相互乒乓球 fornotifywait。JVM 可能会在线程被阻塞时将等待优先级的线程授予监视器。

于 2014-11-17T19:10:51.377 回答
2

无需深入研究您的代码,我可以看到您正在使用单个条件变量来实现一个包含一个生产者和多个消费者的队列。这是一个麻烦的秘诀:如果只有一个条件变量,那么当消费者调用 时notify(),就无法知道它会唤醒生产者还是唤醒另一个消费者。

有两种方法可以摆脱这个陷阱:最简单的方法是始终使用notifyAll().

另一种方法是停止使用synchronizedwait()notify(),而是使用 java.util.concurrent.locks 中的工具。

单个 ReentrantLock 对象可以为您提供两个(或更多)条件变量。生产者专用一个通知消费者,消费者专用一个通知生产者。

注意:当您切换到使用 ReentrantLocks 时,名称会发生​​变化: o.wait()变为c.await(),并o.notify()变为c.signal()

于 2014-11-17T19:43:45.733 回答