ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ReadHoldingWriteLock.java
(Generate patch)

Comparing jsr166/src/test/loops/ReadHoldingWriteLock.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.8 by jsr166, Mon Aug 10 03:13:33 2015 UTC

# Line 1 | Line 1
1 + /*
2 + * Written by Doug Lea with assistance from members of JCP JSR-166
3 + * Expert Group and released to the public domain, as explained at
4 + * http://creativecommons.org/publicdomain/zero/1.0/
5 + */
6   import java.util.concurrent.locks.*;
7  
8   class ReadHoldingWriteLock {
# Line 21 | Line 26 | class ReadHoldingWriteLock {
26      /**
27       * Readlocks succeed after a writing thread unlocks
28       */
29 <    public void testReadAfterWriteLock() throws Exception {
30 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
31 <        lock.writeLock().lock();
32 <        Thread t1 = new Thread(new Runnable() {
29 >    public void testReadAfterWriteLock() throws Exception {
30 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
31 >        lock.writeLock().lock();
32 >        Thread t1 = new Thread(new Runnable() {
33                  public void run() {
34                      lock.readLock().lock();
35                      lock.readLock().unlock();
36 <                }
37 <            });
38 <        Thread t2 = new Thread(new Runnable() {
36 >                }
37 >            });
38 >        Thread t2 = new Thread(new Runnable() {
39                  public void run() {
40                      lock.readLock().lock();
41                      lock.readLock().unlock();
42 <                }
43 <            });
42 >                }
43 >            });
44  
45          t1.start();
46          t2.start();
# Line 45 | Line 50 | class ReadHoldingWriteLock {
50          t2.join(MEDIUM_DELAY_MS);
51          assertTrue(!t1.isAlive());
52          assertTrue(!t2.isAlive());
53 <        
49 <    }
53 >    }
54  
55      /**
56       * Read trylock succeeds if write locked by current thread
57       */
58 <    public void testReadHoldingWriteLock()throws Exception {
59 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
60 <        lock.writeLock().lock();
58 >    public void testReadHoldingWriteLock()throws Exception {
59 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
60 >        lock.writeLock().lock();
61          assertTrue(lock.readLock().tryLock());
62          lock.readLock().unlock();
63          lock.writeLock().unlock();
64 <    }
64 >    }
65  
66      /**
67       * Read lock succeeds if write locked by current thread even if
68       * other threads are waiting
69       */
70 <    public void testReadHoldingWriteLock2() throws Exception{
71 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
72 <        lock.writeLock().lock();
73 <        Thread t1 = new Thread(new Runnable() {
70 >    public void testReadHoldingWriteLock2() throws Exception {
71 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
72 >        lock.writeLock().lock();
73 >        Thread t1 = new Thread(new Runnable() {
74                  public void run() {
75                      lock.readLock().lock();
76                      lock.readLock().unlock();
77 <                }
78 <            });
79 <        Thread t2 = new Thread(new Runnable() {
77 >                }
78 >            });
79 >        Thread t2 = new Thread(new Runnable() {
80                  public void run() {
81                      lock.readLock().lock();
82                      lock.readLock().unlock();
83 <                }
84 <            });
85 <        
83 >                }
84 >            });
85 >
86          t1.start();
87          t2.start();
88          lock.readLock().lock();
# Line 91 | Line 95 | class ReadHoldingWriteLock {
95          t2.join(MEDIUM_DELAY_MS);
96          assertTrue(!t1.isAlive());
97          assertTrue(!t2.isAlive());
98 <    }
98 >    }
99  
100      /**
101       * Fair Read trylock succeeds if write locked by current thread
102       */
103 <    public void testReadHoldingWriteLockFair() throws Exception{
104 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
105 <        lock.writeLock().lock();
103 >    public void testReadHoldingWriteLockFair() throws Exception {
104 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
105 >        lock.writeLock().lock();
106          assertTrue(lock.readLock().tryLock());
107          lock.readLock().unlock();
108          lock.writeLock().unlock();
109 <    }
109 >    }
110  
111      /**
112       * Fair Read lock succeeds if write locked by current thread even if
113       * other threads are waiting
114       */
115 <    public void testReadHoldingWriteLockFair2() throws Exception {
116 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
117 <        lock.writeLock().lock();
118 <        Thread t1 = new Thread(new Runnable() {
115 >    public void testReadHoldingWriteLockFair2() throws Exception {
116 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
117 >        lock.writeLock().lock();
118 >        Thread t1 = new Thread(new Runnable() {
119                  public void run() {
120                      lock.readLock().lock();
121                      lock.readLock().unlock();
122 <                }
123 <            });
124 <        Thread t2 = new Thread(new Runnable() {
122 >                }
123 >            });
124 >        Thread t2 = new Thread(new Runnable() {
125                  public void run() {
126                      lock.readLock().lock();
127                      lock.readLock().unlock();
128 <                }
129 <            });
128 >                }
129 >            });
130  
131          t1.start();
132          t2.start();
# Line 136 | Line 140 | class ReadHoldingWriteLock {
140          t2.join(MEDIUM_DELAY_MS);
141          assertTrue(!t1.isAlive());
142          assertTrue(!t2.isAlive());
143 <        
140 <
141 <    }
143 >    }
144   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines