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 { |
9 |
|
10 |
public static void main(String[] args) throws Exception { |
11 |
ReadHoldingWriteLock t = new ReadHoldingWriteLock(); |
12 |
t.testReadAfterWriteLock(); |
13 |
t.testReadHoldingWriteLock(); |
14 |
t.testReadHoldingWriteLock2(); |
15 |
t.testReadHoldingWriteLockFair(); |
16 |
t.testReadHoldingWriteLockFair2(); |
17 |
} |
18 |
|
19 |
static final long SHORT_DELAY_MS = 50; |
20 |
static final long MEDIUM_DELAY_MS = 200; |
21 |
|
22 |
void assertTrue(boolean b) { |
23 |
if (!b) throw new Error(); |
24 |
} |
25 |
|
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() { |
33 |
public void run() { |
34 |
lock.readLock().lock(); |
35 |
lock.readLock().unlock(); |
36 |
} |
37 |
}); |
38 |
Thread t2 = new Thread(new Runnable() { |
39 |
public void run() { |
40 |
lock.readLock().lock(); |
41 |
lock.readLock().unlock(); |
42 |
} |
43 |
}); |
44 |
|
45 |
t1.start(); |
46 |
t2.start(); |
47 |
Thread.sleep(SHORT_DELAY_MS); |
48 |
lock.writeLock().unlock(); |
49 |
t1.join(MEDIUM_DELAY_MS); |
50 |
t2.join(MEDIUM_DELAY_MS); |
51 |
assertTrue(!t1.isAlive()); |
52 |
assertTrue(!t2.isAlive()); |
53 |
|
54 |
} |
55 |
|
56 |
/** |
57 |
* Read trylock succeeds if write locked by current thread |
58 |
*/ |
59 |
public void testReadHoldingWriteLock()throws Exception { |
60 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
61 |
lock.writeLock().lock(); |
62 |
assertTrue(lock.readLock().tryLock()); |
63 |
lock.readLock().unlock(); |
64 |
lock.writeLock().unlock(); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Read lock succeeds if write locked by current thread even if |
69 |
* other threads are waiting |
70 |
*/ |
71 |
public void testReadHoldingWriteLock2() throws Exception { |
72 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
73 |
lock.writeLock().lock(); |
74 |
Thread t1 = new Thread(new Runnable() { |
75 |
public void run() { |
76 |
lock.readLock().lock(); |
77 |
lock.readLock().unlock(); |
78 |
} |
79 |
}); |
80 |
Thread t2 = new Thread(new Runnable() { |
81 |
public void run() { |
82 |
lock.readLock().lock(); |
83 |
lock.readLock().unlock(); |
84 |
} |
85 |
}); |
86 |
|
87 |
t1.start(); |
88 |
t2.start(); |
89 |
lock.readLock().lock(); |
90 |
lock.readLock().unlock(); |
91 |
Thread.sleep(SHORT_DELAY_MS); |
92 |
lock.readLock().lock(); |
93 |
lock.readLock().unlock(); |
94 |
lock.writeLock().unlock(); |
95 |
t1.join(MEDIUM_DELAY_MS); |
96 |
t2.join(MEDIUM_DELAY_MS); |
97 |
assertTrue(!t1.isAlive()); |
98 |
assertTrue(!t2.isAlive()); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Fair Read trylock succeeds if write locked by current thread |
103 |
*/ |
104 |
public void testReadHoldingWriteLockFair() throws Exception { |
105 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
106 |
lock.writeLock().lock(); |
107 |
assertTrue(lock.readLock().tryLock()); |
108 |
lock.readLock().unlock(); |
109 |
lock.writeLock().unlock(); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Fair Read lock succeeds if write locked by current thread even if |
114 |
* other threads are waiting |
115 |
*/ |
116 |
public void testReadHoldingWriteLockFair2() throws Exception { |
117 |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
118 |
lock.writeLock().lock(); |
119 |
Thread t1 = new Thread(new Runnable() { |
120 |
public void run() { |
121 |
lock.readLock().lock(); |
122 |
lock.readLock().unlock(); |
123 |
} |
124 |
}); |
125 |
Thread t2 = new Thread(new Runnable() { |
126 |
public void run() { |
127 |
lock.readLock().lock(); |
128 |
lock.readLock().unlock(); |
129 |
} |
130 |
}); |
131 |
|
132 |
t1.start(); |
133 |
t2.start(); |
134 |
lock.readLock().lock(); |
135 |
lock.readLock().unlock(); |
136 |
Thread.sleep(SHORT_DELAY_MS); |
137 |
lock.readLock().lock(); |
138 |
lock.readLock().unlock(); |
139 |
lock.writeLock().unlock(); |
140 |
t1.join(MEDIUM_DELAY_MS); |
141 |
t2.join(MEDIUM_DELAY_MS); |
142 |
assertTrue(!t1.isAlive()); |
143 |
assertTrue(!t2.isAlive()); |
144 |
} |
145 |
} |