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.3 by jsr166, Thu Oct 29 23:09:08 2009 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/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6   import java.util.concurrent.locks.*;
7  
# Line 27 | Line 27 | class ReadHoldingWriteLock {
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() {
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 50 | Line 50 | class ReadHoldingWriteLock {
50          t2.join(MEDIUM_DELAY_MS);
51          assertTrue(!t1.isAlive());
52          assertTrue(!t2.isAlive());
53
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();
59 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
60 >        lock.writeLock().lock();
61          assertTrue(lock.readLock().tryLock());
62          lock.readLock().unlock();
63          lock.writeLock().unlock();
# Line 68 | Line 67 | class ReadHoldingWriteLock {
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 <            });
83 >                }
84 >            });
85  
86          t1.start();
87          t2.start();
# Line 101 | Line 100 | class ReadHoldingWriteLock {
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();
# Line 114 | Line 113 | class ReadHoldingWriteLock {
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() {
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 141 | Line 140 | class ReadHoldingWriteLock {
140          t2.join(MEDIUM_DELAY_MS);
141          assertTrue(!t1.isAlive());
142          assertTrue(!t2.isAlive());
144
145
143      }
144   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines