[concurrency-interest] maximum number of shared lock forReentrantReadWriteLock

Tim Peierls tim at peierls.net
Thu Jul 7 13:49:04 EDT 2005


Tim Peierls wrote:
>>> If acquires is 0x7FFF, then that's the maximum of 65535 that Tim 
>>> mentioned below, so throwing an exception is correct in this case.
>>
>> 0x7FFF is 32767 -- now I think Jinsong Hu is right. You get an 
>> overflow exception when acquiring the 32768th read lock.
> 
> And if so, this is either a doc bug or code bug. As a doc bug it could 
> be fixed by changing 65536 to 32767. As a code bug it could be fixed by 
> changing the overflow test.
> 
> But maybe this has already been caught and fixed?

Hmm, doesn't look like it has. Here's a program that confirms the effective 
maximum counts:

import java.util.concurrent.locks.*;

public class ReadWriteOverflow {
     public static void main(String... args) {
         testWriteOverflow();
         testReadOverflow();
     }

     public static void testWriteOverflow() {
         ReadWriteLock rwl = new ReentrantReadWriteLock();
         for (int i = 1; i <= WMAX; ++i)
             rwl.writeLock().lock();
         System.out.printf(
             "%d write lock acquisitions, trying one more...%n",
             WMAX);
         try {
             rwl.writeLock().lock();
             System.out.printf(
                 "%d write lock acquisitions%n", WMAX+1);
         } catch (Error e) {
             System.out.println(e);
         }
     }

     public static void testReadOverflow() {
         ReadWriteLock rwl = new ReentrantReadWriteLock();
         for (int i = 1; i <= RMAX; ++i)
             rwl.readLock().lock();
         System.out.printf(
             "%d read lock acquisitions, trying one more...%n",
             RMAX);
         try {
             rwl.readLock().lock();
             System.out.printf(
                 "%d read lock acquisitions%n", RMAX+1);
         } catch (Error e) {
             System.out.println(e);
         }
     }

     static final int RMAX = 32767;
     static final int WMAX = 65535;
}

I get this output:

65535 write lock acquisitions, trying one more...
java.lang.Error: Maximum lock count exceeded
32767 read lock acquisitions, trying one more...
java.lang.Error: Maximum lock count exceeded

--tim



More information about the Concurrency-interest mailing list