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 |
|
|
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(); |
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(); |
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(); |
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(); |
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(); |
140 |
|
t2.join(MEDIUM_DELAY_MS); |
141 |
|
assertTrue(!t1.isAlive()); |
142 |
|
assertTrue(!t2.isAlive()); |
144 |
– |
|
145 |
– |
|
143 |
|
} |
144 |
|
} |