Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6305337

Reentrant writeLock in Fair mode ReentrantReadWriteLock may block

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 6
    • core-libs
    • b51
    • generic
    • generic
    • Verified

      Doug Lea writes:
      ----------------------------------------------------------------
      Contrary to expectations, a recursive writelock attempt
      may block if there are other waiting threads. This is both a flaw
      in the spec (which was not clear about this case) and code.

      Test case:

      import java.util.concurrent.locks.*;
      import java.util.concurrent.*;

      public class T720 {
           static ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
           static Thread t1 = new Thread() {
                   public void run() {
                       try {
                           if (!rl.readLock().tryLock(2000,
      TimeUnit.MILLISECONDS))
                               throw new Error("RRWL ordering error");
                           Thread.sleep(2000);
                           if (!rl.readLock().tryLock(2000,
      TimeUnit.MILLISECONDS))
                               throw new Error("RRWL ordering error");
                           rl.readLock().unlock();
                           rl.readLock().unlock();
                       } catch(InterruptedException ignore) {}

                   }
               };


           static Thread t2 = new Thread() {
                   public void run() {
                       try {
                           Thread.sleep(1000);
                           if (!rl.writeLock().tryLock(2000,
      TimeUnit.MILLISECONDS))
                               throw new Error("RRWL ordering error");
                           Thread.sleep(1000);
                           if (!rl.writeLock().tryLock(2000,
      TimeUnit.MILLISECONDS))
                               throw new Error("RRWL ordering error");
                       } catch(InterruptedException ignore) {}
                   }
               };

           public static void main(String[] args) {
               t1.start();
               t2.start();
           }
      }

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: