经典的读者 - 编写者算法 - 使用单一锁定有什么问题?

时间:2015-10-02 06:32:45

标签: java multithreading algorithm

我在问经典的读者 - 作家问题:wiki link

我最近正在研究这个问题,而且我只使用一个锁就写了一个解决方案:

class ReadWrite {
    private Object lock = new Object();
    private boolean isWriting = false;
    private int readerCount = 0;

    public void read() throws InterruptedException {
        synchronized(lock) {
            while (isWriting) {
                lock.wait();
            }
            readerCount++;
        }
        doRead(); // takes some time to read it...
        synchronized(lock) {
            readerCount--;
            lock.notifyAll();
        }
    }

    public void write(int value) throws InterruptedException {
        synchronized(lock) {
            while (readerCount > 0 || isWriting) {
                lock.wait();
            }
            isWriting = true;
        }
        doWrite(value); // takes some time to write it
        synchronized(lock) {
            isWriting = false;
            lock.notifyAll();
        }
    }

    private void doRead() { // omitted }
    private void doWrite(int value) { // omitted }
}

..然后我会创建一堆线程,每个线程重复调用read()或write()。

解决了有利于读者的 第一 读者/作者问题。

...然后我看到所有在线/书籍的引用总是使用两个/多个锁;所以我不确定我是否遗漏了任何东西

我测试了多个线程配置,它实际上工作正常,如: 1)同时多次读取 2)只有一位作家在没有阅读的情况下随时写作

我解决了 3rd 读者/作者问题(读者/作者之间的公平性)仅稍微增加了上述解决方案,您可以看到/ *编辑* /以下)

除了可能的效率差异之外,你是否看到任何错误的算法错误/使用单一锁定读者/作者问题 (至少对于有利于读者的第一读者/作者问题)

编辑:

有一个关于我是否可以通过修改原始问题来解决第三个读者/作者问题的讨论...并将其限制在2000ms以下(100ms读/写* 20个线程),所以这里是;您可以取消注释所有sysout以查看是否仍然发生独占写入,以及同时读取。我还在下面复制粘贴的时间运行输出。

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;

class ReadWrite {
   private Object lock = new Object();
   private boolean isWriting = false;
   private int readerCount = 0;

   Queue<Thread> order = new LinkedList<Thread>();

   public void read(Reader r) throws InterruptedException {
      long start = System.currentTimeMillis();

      synchronized (lock) {
         while (isWriting || (!order.isEmpty() && order.peek() != r)) {

            if (!order.contains(r)) {
               order.add(r);
            }

            lock.wait();
         }

         order.remove(r);
         readerCount++;
         lock.notifyAll();
         // System.out.println("Reader " + r.id + " started reading; readerCount: " + readerCount);
      }

      doRead(r.id); // takes however many seconds to read it...

      synchronized (lock) {
         readerCount--;
         // System.out.println("Reader " + r.id + " finished reading; readerCount: " + readerCount);
         lock.notifyAll();
      }

      long delta = System.currentTimeMillis() - start;
      System.out.println("Reader " + r.id + " delta: " + delta + "");
   }

   public void write(Writer w, int value) throws InterruptedException {
      // System.out.println("Writer " + w.id + " entered write");
      long start = System.currentTimeMillis();

      synchronized (lock) {
         while (readerCount > 0 || isWriting ||  (!order.isEmpty() && order.peek() != w)) {

            if (!order.contains(w)) {
               order.add(w);
            }

            lock.wait();
         }

         order.remove(w);
         isWriting = true;
         // System.out.println("Writer " + w.id + " started writing-----------");
      }

      doWrite(w.id, value);

      synchronized (lock) {
         isWriting = false;

         // System.out.println("Writer " + w.id + " finished writing----------");
         lock.notifyAll();
      }

      long delta = System.currentTimeMillis() - start;
      System.out.println("Writer " + w.id + " delta: " + delta);

      if (delta - 2000 > 10) {
         System.out.println("!!!attention above!!!");
      }
   }

   private void doRead(int readerId) {
      LockSupport.parkNanos(100_000_000L);
   }

   private void doWrite(int writerId, int value) {
      LockSupport.parkNanos(100_000_000L);
   }
}

class Reader extends Thread{
   public int id = 0;
   private ReadWrite rw = null;
   public Reader(int id, ReadWrite rw) {
      this.id = id;
      this.rw = rw;
   }

   @Override
   public void run() {
      while (true) {
         try {
            rw.read(this);
         } catch (InterruptedException e) {
         }
      }
   }
}

class Writer extends Thread {
   public int id = 0;
   private ReadWrite rw = null;

   public Writer(int id, ReadWrite rw) {
      this.id = id;
      this.rw = rw;
   }

   @Override
   public void run() {
      while (true) {
         try {
            rw.write(this, 0);
         } catch (InterruptedException e) {
         }  
      }
   }
}

public class RW {
   public static void main(String[] args) throws InterruptedException {
      ReadWrite rw = new ReadWrite();

      for (int i : IntStream.range(0, 10).toArray()) {
         new Reader(i, rw).start();
      }

      for (int i : IntStream.range(0, 10).toArray()) {
         new Writer(i, rw).start();
      }
   }
}

扩展运行的输出:

Reader 7 delta: 100
Reader 8 delta: 100
Reader 9 delta: 100
Reader 3 delta: 101
Reader 2 delta: 100
Reader 5 delta: 100
Reader 1 delta: 101
Reader 0 delta: 101
Reader 6 delta: 100
Reader 4 delta: 100
Writer 0 delta: 200
Writer 1 delta: 302
Writer 3 delta: 402
Writer 5 delta: 502
Writer 2 delta: 603
Writer 4 delta: 704
Writer 6 delta: 805
Writer 7 delta: 906
Writer 8 delta: 1006
Writer 9 delta: 1106
Reader 8 delta: 1106
Reader 9 delta: 1106
Reader 7 delta: 1106
Reader 5 delta: 1105
Reader 6 delta: 1105
Reader 3 delta: 1106
Reader 2 delta: 1105
Reader 1 delta: 1105
Reader 0 delta: 1105
Reader 4 delta: 1105
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1106
Writer 5 delta: 1106
Writer 2 delta: 1105
Writer 4 delta: 1105
Writer 6 delta: 1104
Writer 7 delta: 1104
Writer 8 delta: 1104
Writer 9 delta: 1105
Reader 8 delta: 1105
Reader 7 delta: 1106
Reader 9 delta: 1106
Reader 3 delta: 1107
Reader 5 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1107
Reader 1 delta: 1108
Reader 0 delta: 1108
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1109
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 8 delta: 1110
Reader 9 delta: 1109
Reader 7 delta: 1110
Reader 3 delta: 1110
Reader 5 delta: 1111
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1110
Reader 0 delta: 1110
Reader 1 delta: 1111
Writer 0 delta: 1111
Writer 1 delta: 1112
Writer 3 delta: 1111
Writer 5 delta: 1112
Writer 2 delta: 1113
Writer 4 delta: 1111
Writer 6 delta: 1112
Writer 7 delta: 1111
Writer 8 delta: 1112
Writer 9 delta: 1112
Reader 8 delta: 1111
Reader 9 delta: 1111
Reader 7 delta: 1111
Reader 5 delta: 1110
Reader 3 delta: 1111
Reader 6 delta: 1110
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 1 delta: 1110
Reader 0 delta: 1110
Writer 0 delta: 1109
Writer 1 delta: 1109
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1108
Writer 9 delta: 1107
Reader 3 delta: 1105
Reader 7 delta: 1106
Reader 4 delta: 1105
Reader 6 delta: 1105
Reader 8 delta: 1107
Reader 9 delta: 1106
Reader 2 delta: 1105
Reader 1 delta: 1105
Reader 0 delta: 1105
Reader 5 delta: 1107
Writer 0 delta: 1106
Writer 1 delta: 1105
Writer 3 delta: 1106
Writer 5 delta: 1105
Writer 2 delta: 1104
Writer 4 delta: 1103
Writer 6 delta: 1103
Writer 7 delta: 1103
Writer 8 delta: 1103
Writer 9 delta: 1104
Reader 3 delta: 1104
Reader 7 delta: 1105
Reader 8 delta: 1106
Reader 1 delta: 1105
Reader 4 delta: 1106
Reader 6 delta: 1106
Reader 0 delta: 1105
Reader 2 delta: 1105
Reader 9 delta: 1106
Reader 5 delta: 1105
Writer 0 delta: 1105
Writer 1 delta: 1105
Writer 3 delta: 1104
Writer 5 delta: 1105
Writer 2 delta: 1106
Writer 4 delta: 1107
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 7 delta: 1110
Reader 3 delta: 1111
Reader 8 delta: 1110
Reader 1 delta: 1111
Reader 4 delta: 1111
Reader 6 delta: 1112
Reader 0 delta: 1112
Reader 5 delta: 1112
Reader 2 delta: 1112
Reader 9 delta: 1112
Writer 0 delta: 1113
Writer 1 delta: 1113
Writer 3 delta: 1113
Writer 5 delta: 1113
Writer 2 delta: 1113
Writer 4 delta: 1112
Writer 6 delta: 1110
Writer 7 delta: 1111
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 7 delta: 1110
Reader 3 delta: 1111
Reader 8 delta: 1110
Reader 1 delta: 1110
Reader 4 delta: 1110
Reader 6 delta: 1109
Reader 0 delta: 1110
Reader 5 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Writer 0 delta: 1109
Writer 1 delta: 1111
Writer 3 delta: 1112
Writer 5 delta: 1111
Writer 2 delta: 1111
Writer 4 delta: 1111
Writer 6 delta: 1112
Writer 7 delta: 1112
Writer 8 delta: 1112
Writer 9 delta: 1111
Reader 7 delta: 1111
Reader 3 delta: 1110
Reader 8 delta: 1111
Reader 4 delta: 1112
Reader 1 delta: 1112
Reader 5 delta: 1112
Reader 6 delta: 1113
Reader 0 delta: 1112
Reader 2 delta: 1113
Reader 9 delta: 1113
Writer 0 delta: 1112
Writer 1 delta: 1111
Writer 3 delta: 1110
Writer 5 delta: 1110
Writer 2 delta: 1109
Writer 4 delta: 1109
Writer 6 delta: 1108
Writer 7 delta: 1108
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 7 delta: 1109
Reader 3 delta: 1109
Reader 1 delta: 1107
Reader 4 delta: 1107
Reader 8 delta: 1109
Reader 9 delta: 1106
Reader 0 delta: 1107
Reader 2 delta: 1106
Reader 6 delta: 1107
Reader 5 delta: 1107
Writer 0 delta: 1107
Writer 1 delta: 1106
Writer 3 delta: 1107
Writer 5 delta: 1109
Writer 2 delta: 1110
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1110
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 7 delta: 1109
Reader 1 delta: 1109
Reader 3 delta: 1110
Reader 4 delta: 1109
Reader 8 delta: 1110
Reader 0 delta: 1109
Reader 9 delta: 1109
Reader 6 delta: 1109
Reader 2 delta: 1109
Reader 5 delta: 1109
Writer 0 delta: 1109
Writer 1 delta: 1110
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1108
Writer 4 delta: 1109
Writer 6 delta: 1108
Writer 7 delta: 1109
Writer 8 delta: 1111
Writer 9 delta: 1111
Reader 1 delta: 1111
Reader 7 delta: 1112
Reader 3 delta: 1111
Reader 4 delta: 1111
Reader 8 delta: 1111
Reader 9 delta: 1111
Reader 6 delta: 1111
Reader 0 delta: 1111
Reader 2 delta: 1112
Reader 5 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1110
Writer 8 delta: 1108
Writer 9 delta: 1107
Reader 1 delta: 1107
Reader 7 delta: 1108
Reader 3 delta: 1108
Reader 4 delta: 1109
Reader 8 delta: 1108
Reader 0 delta: 1109
Reader 6 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Reader 5 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1108
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1107
Writer 8 delta: 1107
Writer 9 delta: 1108
Reader 1 delta: 1108
Reader 4 delta: 1107
Reader 7 delta: 1108
Reader 3 delta: 1108
Reader 8 delta: 1108
Reader 6 delta: 1107
Reader 0 delta: 1107
Reader 5 delta: 1107
Reader 9 delta: 1108
Reader 2 delta: 1107
Writer 0 delta: 1107
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1109
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 1 delta: 1109
Reader 3 delta: 1109
Reader 4 delta: 1109
Reader 7 delta: 1109
Reader 0 delta: 1109
Reader 5 delta: 1108
Reader 8 delta: 1109
Reader 6 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Writer 0 delta: 1110
Writer 1 delta: 1111
Writer 3 delta: 1111
Writer 5 delta: 1110
Writer 2 delta: 1110
Writer 4 delta: 1111
Writer 6 delta: 1111
Writer 7 delta: 1112
Writer 8 delta: 1111
Writer 9 delta: 1110
Reader 1 delta: 1111
Reader 3 delta: 1110
Reader 4 delta: 1111
Reader 0 delta: 1111
Reader 5 delta: 1111
Reader 7 delta: 1112
Reader 8 delta: 1112
Reader 6 delta: 1112
Reader 2 delta: 1112
Reader 9 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1110
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 1 delta: 1111
Reader 3 delta: 1112
Reader 4 delta: 1111
Reader 5 delta: 1110
Reader 0 delta: 1110
Reader 7 delta: 1112
Reader 8 delta: 1111
Reader 6 delta: 1111
Reader 9 delta: 1110
Reader 2 delta: 1110
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1112
Writer 5 delta: 1112
Writer 2 delta: 1112
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1111
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 1 delta: 1109
Reader 3 delta: 1109
Reader 5 delta: 1109
Reader 4 delta: 1109
Reader 7 delta: 1108
Reader 8 delta: 1108
Reader 0 delta: 1110
Reader 6 delta: 1108
Reader 9 delta: 1108
Reader 2 delta: 1108
Writer 0 delta: 1107
Writer 1 delta: 1108
Writer 3 delta: 1107
Writer 5 delta: 1107
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1106
Writer 8 delta: 1106
Writer 9 delta: 1105
Reader 1 delta: 1105
Reader 3 delta: 1105
Reader 5 delta: 1106
Reader 4 delta: 1106
Reader 7 delta: 1105
Reader 0 delta: 1106
Reader 8 delta: 1106
Reader 6 delta: 1106
Reader 9 delta: 1106
Reader 2 delta: 1106
Writer 0 delta: 1107
Writer 1 delta: 1106
Writer 3 delta: 1106
Writer 5 delta: 1106
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1107
Writer 8 delta: 1108
Writer 9 delta: 1108
Reader 1 delta: 1109
Reader 3 delta: 1108
Reader 5 delta: 1108
Reader 7 delta: 1109
Reader 4 delta: 1109
Reader 8 delta: 1108
Reader 0 delta: 1108
Reader 9 delta: 1109
Reader 2 delta: 1109
Reader 6 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1108
Writer 3 delta: 1109
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1108
Writer 6 delta: 1107
Writer 7 delta: 1108
Writer 8 delta: 1107
Writer 9 delta: 1107
Reader 1 delta: 1106
Reader 3 delta: 1107
Reader 5 delta: 1107
Reader 7 delta: 1106
Reader 0 delta: 1108
Reader 8 delta: 1108
Reader 4 delta: 1108
Reader 9 delta: 1108
Reader 2 delta: 1108
Reader 6 delta: 1108
Writer 0 delta: 1109
Writer 1 delta: 1111
Writer 3 delta: 1111
Writer 5 delta: 1111
Writer 2 delta: 1110
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1111
Writer 8 delta: 1112
Writer 9 delta: 1112
Reader 1 delta: 1113
Reader 3 delta: 1112
Reader 5 delta: 1112
Reader 0 delta: 1110
Reader 7 delta: 1112
Reader 8 delta: 1111
Reader 9 delta: 1110
Reader 4 delta: 1111
Reader 6 delta: 1110
Reader 2 delta: 1110
Writer 0 delta: 1110
Writer 1 delta: 1108
Writer 3 delta: 1107
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1108
Writer 6 delta: 1108
Writer 7 delta: 1107
Writer 8 delta: 1106
Writer 9 delta: 1107
Reader 1 delta: 1106
Reader 3 delta: 1106
Reader 5 delta: 1105
Reader 0 delta: 1106
Reader 7 delta: 1106
Reader 8 delta: 1106
Reader 4 delta: 1106
Reader 9 delta: 1106
Reader 6 delta: 1106
Reader 2 delta: 1107
Writer 0 delta: 1106
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1109
Writer 7 delta: 1110
Writer 8 delta: 1111
Writer 9 delta: 1110
Reader 3 delta: 1110
Reader 5 delta: 1110
Reader 1 delta: 1110
Reader 7 delta: 1110
Reader 0 delta: 1110
Reader 4 delta: 1110
Reader 8 delta: 1110
Reader 9 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1111
Writer 0 delta: 1110
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1110
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 3 delta: 1111
Reader 0 delta: 1111
Reader 5 delta: 1112
Reader 7 delta: 1111
Reader 1 delta: 1112
Reader 9 delta: 1110
Reader 8 delta: 1110
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1110
Writer 0 delta: 1110
Writer 1 delta: 1110
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1107
Writer 6 delta: 1108
Writer 7 delta: 1107
Writer 8 delta: 1107
Writer 9 delta: 1108
Reader 3 delta: 1107
Reader 1 delta: 1107
Reader 9 delta: 1107
Reader 0 delta: 1107
Reader 7 delta: 1107
Reader 5 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1108
Reader 8 delta: 1108
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1107
Writer 4 delta: 1108
Writer 6 delta: 1107
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1108
Reader 3 delta: 1108
Reader 1 delta: 1108
Reader 9 delta: 1108
Reader 7 delta: 1109
Reader 0 delta: 1109
Reader 5 delta: 1109
Reader 8 delta: 1109
Reader 2 delta: 1109
Reader 6 delta: 1109
Reader 4 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1108
Writer 6 delta: 1108
Writer 7 delta: 1106
Writer 8 delta: 1107
Writer 9 delta: 1107
Reader 1 delta: 1107
Reader 3 delta: 1108
Reader 9 delta: 1107
Reader 0 delta: 1107
Reader 5 delta: 1107
Reader 7 delta: 1107
Reader 8 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1107
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1107
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1112
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 3 delta: 1111
Reader 1 delta: 1111
Reader 5 delta: 1111
Reader 9 delta: 1112
Reader 0 delta: 1111
Reader 7 delta: 1112
Reader 6 delta: 1112
Reader 2 delta: 1112
Reader 4 delta: 1112
Reader 8 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1113
Writer 5 delta: 1112
Writer 2 delta: 1112
Writer 4 delta: 1111
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 3 delta: 1108
Reader 1 delta: 1108
Reader 9 delta: 1108
Reader 0 delta: 1108
Reader 5 delta: 1108
Reader 2 delta: 1108
Reader 7 delta: 1109
Reader 4 delta: 1108
Reader 6 delta: 1108
Reader 8 delta: 1108
Writer 0 delta: 1110
Writer 1 delta: 1109
Writer 3 delta: 1107
Writer 5 delta: 1107
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1108
Writer 8 delta: 1108
Writer 9 delta: 1108
Reader 3 delta: 1109
Reader 9 delta: 1109
Reader 1 delta: 1110
Reader 7 delta: 1108
Reader 4 delta: 1108
Reader 2 delta: 1108
Reader 0 delta: 1110
Reader 8 delta: 1108
Reader 5 delta: 1110
Reader 6 delta: 1108
Writer 0 delta: 1106
Writer 1 delta: 1106
Writer 3 delta: 1106
Writer 5 delta: 1107
Writer 2 delta: 1106
Writer 4 delta: 1106
Writer 6 delta: 1106
Writer 7 delta: 1106
Writer 8 delta: 1106
Writer 9 delta: 1106
Reader 3 delta: 1105
Reader 9 delta: 1105
Reader 1 delta: 1105
Reader 4 delta: 1105
Reader 7 delta: 1105
Reader 2 delta: 1105
Reader 8 delta: 1105
Reader 0 delta: 1105
Reader 5 delta: 1105
Reader 6 delta: 1105
Writer 0 delta: 1106
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1107
Writer 2 delta: 1108

所以它在最大预期时间内受限;

1 个答案:

答案 0 :(得分:1)

似乎你的代码仍然没有解决第三个问题,并且优先考虑读者而不是作者。正如维基所说:

  

因此,有时会提出第三个读者 - 作者问题,这增加了不允许线程饿死的约束;也就是说,获取对共享数据的锁定的操作将始终在有限的时间内终止。

让我们创建10个读者和10个作者线程:

public static void main(String[] args) throws InterruptedException {
    ReadWrite rw = new ReadWrite();
    for(int i : IntStream.range(0, 10).toArray()) {
        new Thread(() -> {
            while(true) {
                try {
                    rw.read(i);
                } catch (InterruptedException e) {}
            }
        }, "Reader #"+i).start();
    }
    for(int i : IntStream.range(0, 10).toArray()) {
        new Thread(() -> {
            while(true) {
                try {
                    rw.write(i, i);
                } catch (InterruptedException e) {}
            }
        }, "Writer #"+i).start();
    }
}

doReaddoWrite只睡100毫秒:

private void doRead(int readerId) { 
    LockSupport.parkNanos(100_000_000L);
}

private void doWrite(int writerId, int value) {
    LockSupport.parkNanos(100_000_000L);
}

让我们打印执行写入所需的时间:

public void write(int writerId, int value) throws InterruptedException {
   long start = System.currentTimeMillis();
   ... all your code ...
   long delta = System.currentTimeMillis()-start;
   System.out.println("Write delta: "+delta);
}

我删除了其他打印件。现在我看到以下输出:

Write delta: 200
Write delta: 200
Write delta: 600
Write delta: 800
Write delta: 1000
Write delta: 1099
Write delta: 1300
Write delta: 1500
Write delta: 600
Write delta: 1899
...
Write delta: 2700
Write delta: 4399
Write delta: 2100
Write delta: 1900
...
Write delta: 2800
Write delta: 3400
Write delta: 6999
Write delta: 900
Write delta: 1300
Write delta: 2500
...
Write delta: 1800
Write delta: 2800
Write delta: 7802
Write delta: 2600
...

我希望最大增量大约为2000毫秒:我们有20个并发线程,每个线程需要大约100毫秒才能运行,因此我们应该等待所有其他线程完成当前操作。但我们不应该多次等待其他一些线程。实际上,当我用标准JDK ReentrantReadWriteLock替换你的实现时,我观察到最大delta 2000-2100。在您的情况下,延迟可能会延长几倍,并且似乎没有保证最大值(在较长时间内我也观察到更高的延迟)。因此,我不会说你的代码解决了第三个问题。