JDK-6305337 : Reentrant writeLock in Fair mode ReentrantReadWriteLock may block
  • Type: Bug
  • Status: Closed
  • Resolution: Fixed
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Priority: P3
  • Affected Version: 6
  • OS: generic
  • CPU: generic
  • Submit Date: 2005-08-02
  • Updated Date: 2010-04-02
  • Resolved Date: 2005-09-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 6
6 b51Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
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();
     }
}

Comments
EVALUATION Fix being provided by Doug Lea
2005-08-02