ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LockSupportTest.java
Revision: 1.16
Committed: Wed Oct 6 04:05:42 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +31 -54 lines
Log Message:
use newStartedThread and awaitTermination

File Contents

# User Rev Content
1 dl 1.1 /*
2 jsr166 1.15 * Written by Doug Lea and Martin Buchholz with assistance from
3     * members of JCP JSR-166 Expert Group and released to the public
4     * domain, as explained at
5 dl 1.5 * http://creativecommons.org/licenses/publicdomain
6 jsr166 1.8 * Other contributors include Andrew Wright, Jeffrey Hayes,
7     * Pat Fisher, Mike Judd.
8 dl 1.1 */
9    
10     import junit.framework.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13 jsr166 1.15 import java.util.concurrent.atomic.AtomicBoolean;
14 dl 1.1 import java.util.concurrent.locks.*;
15    
16 jsr166 1.10 public class LockSupportTest extends JSR166TestCase {
17 dl 1.1 public static void main(String[] args) {
18 jsr166 1.14 junit.textui.TestRunner.run(suite());
19 dl 1.1 }
20 jsr166 1.15
21 dl 1.1 public static Test suite() {
22 jsr166 1.12 return new TestSuite(LockSupportTest.class);
23 dl 1.1 }
24    
25 dl 1.3 /**
26 jsr166 1.15 * park is released by subsequent unpark
27 dl 1.3 */
28 jsr166 1.15 public void testParkBeforeUnpark() throws InterruptedException {
29     final CountDownLatch threadStarted = new CountDownLatch(1);
30 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
31 jsr166 1.12 public void realRun() {
32 jsr166 1.15 threadStarted.countDown();
33 jsr166 1.11 LockSupport.park();
34     }});
35    
36 jsr166 1.15 threadStarted.await();
37     Thread.sleep(SHORT_DELAY_MS);
38     LockSupport.unpark(t);
39 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
40 jsr166 1.15 }
41    
42     /**
43     * parkUntil is released by subsequent unpark
44     */
45     public void testParkUntilBeforeUnpark() throws InterruptedException {
46     final CountDownLatch threadStarted = new CountDownLatch(1);
47 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
48 jsr166 1.15 public void realRun() {
49     long d = new Date().getTime() + LONG_DELAY_MS;
50     long nanos = LONG_DELAY_MS * 1000L * 1000L;
51     long t0 = System.nanoTime();
52     threadStarted.countDown();
53     LockSupport.parkUntil(d);
54     assertTrue(System.nanoTime() - t0 < nanos);
55     }});
56    
57     threadStarted.await();
58     Thread.sleep(SHORT_DELAY_MS);
59     LockSupport.unpark(t);
60 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
61 jsr166 1.15 }
62    
63     /**
64     * parkNanos is released by subsequent unpark
65     */
66     public void testParkNanosBeforeUnpark() throws InterruptedException {
67     final CountDownLatch threadStarted = new CountDownLatch(1);
68 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
69 jsr166 1.15 public void realRun() {
70     long nanos = LONG_DELAY_MS * 1000L * 1000L;
71     long t0 = System.nanoTime();
72     threadStarted.countDown();
73     LockSupport.parkNanos(nanos);
74     assertTrue(System.nanoTime() - t0 < nanos);
75     }});
76    
77     threadStarted.await();
78 jsr166 1.11 Thread.sleep(SHORT_DELAY_MS);
79     LockSupport.unpark(t);
80 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
81 dl 1.1 }
82    
83 dl 1.3 /**
84 jsr166 1.15 * park is released by preceding unpark
85 dl 1.4 */
86 jsr166 1.15 public void testParkAfterUnpark() throws Exception {
87     final CountDownLatch threadStarted = new CountDownLatch(1);
88     final AtomicBoolean unparked = new AtomicBoolean(false);
89 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
90 jsr166 1.15 public void realRun() throws Exception {
91     threadStarted.countDown();
92     while (!unparked.get())
93     Thread.yield();
94 jsr166 1.11 LockSupport.park();
95     }});
96    
97 jsr166 1.15 threadStarted.await();
98 jsr166 1.11 LockSupport.unpark(t);
99 jsr166 1.15 unparked.set(true);
100 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
101 dl 1.4 }
102    
103     /**
104 jsr166 1.15 * parkUntil is released by preceding unpark
105 dl 1.4 */
106 jsr166 1.15 public void testParkUntilAfterUnpark() throws Exception {
107     final CountDownLatch threadStarted = new CountDownLatch(1);
108     final AtomicBoolean unparked = new AtomicBoolean(false);
109 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
110 jsr166 1.15 public void realRun() throws Exception {
111     threadStarted.countDown();
112     while (!unparked.get())
113     Thread.yield();
114     long d = new Date().getTime() + LONG_DELAY_MS;
115     long nanos = LONG_DELAY_MS * 1000L * 1000L;
116     long t0 = System.nanoTime();
117     LockSupport.parkUntil(d);
118     assertTrue(System.nanoTime() - t0 < nanos);
119     }});
120    
121     threadStarted.await();
122     LockSupport.unpark(t);
123     unparked.set(true);
124 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
125 jsr166 1.15 }
126    
127     /**
128     * parkNanos is released by preceding unpark
129     */
130     public void testParkNanosAfterUnpark() throws Exception {
131     final CountDownLatch threadStarted = new CountDownLatch(1);
132     final AtomicBoolean unparked = new AtomicBoolean(false);
133 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
134 jsr166 1.15 public void realRun() throws Exception {
135     threadStarted.countDown();
136     while (!unparked.get())
137     Thread.yield();
138     long nanos = LONG_DELAY_MS * 1000L * 1000L;
139     long t0 = System.nanoTime();
140     LockSupport.parkNanos(nanos);
141     assertTrue(System.nanoTime() - t0 < nanos);
142     }});
143    
144     threadStarted.await();
145     LockSupport.unpark(t);
146     unparked.set(true);
147 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
148 jsr166 1.15 }
149    
150     /**
151     * park is released by subsequent interrupt
152     */
153     public void testParkBeforeInterrupt() throws InterruptedException {
154     final CountDownLatch threadStarted = new CountDownLatch(1);
155 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
156 jsr166 1.12 public void realRun() {
157 jsr166 1.15 assertFalse(Thread.currentThread().isInterrupted());
158     threadStarted.countDown();
159 jsr166 1.11 LockSupport.park();
160 jsr166 1.15 assertTrue(Thread.currentThread().isInterrupted());
161     }});
162    
163     threadStarted.await();
164     Thread.sleep(SHORT_DELAY_MS);
165     t.interrupt();
166 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
167 jsr166 1.15 }
168    
169     /**
170     * parkUntil is released by subsequent interrupt
171     */
172     public void testParkUntilBeforeInterrupt() throws InterruptedException {
173     final CountDownLatch threadStarted = new CountDownLatch(1);
174 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
175 jsr166 1.15 public void realRun() {
176     long d = new Date().getTime() + LONG_DELAY_MS;
177     long nanos = LONG_DELAY_MS * 1000L * 1000L;
178     long t0 = System.nanoTime();
179     threadStarted.countDown();
180     LockSupport.parkUntil(d);
181     assertTrue(System.nanoTime() - t0 < nanos);
182 jsr166 1.11 }});
183    
184 jsr166 1.15 threadStarted.await();
185 jsr166 1.11 Thread.sleep(SHORT_DELAY_MS);
186     t.interrupt();
187 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
188 dl 1.4 }
189    
190     /**
191 jsr166 1.15 * parkNanos is released by subsequent interrupt
192 dl 1.4 */
193 jsr166 1.15 public void testParkNanosBeforeInterrupt() throws InterruptedException {
194     final CountDownLatch threadStarted = new CountDownLatch(1);
195 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
196 jsr166 1.12 public void realRun() {
197 jsr166 1.15 long nanos = LONG_DELAY_MS * 1000L * 1000L;
198     long t0 = System.nanoTime();
199     threadStarted.countDown();
200     LockSupport.parkNanos(nanos);
201     assertTrue(System.nanoTime() - t0 < nanos);
202     }});
203    
204     threadStarted.await();
205     Thread.sleep(SHORT_DELAY_MS);
206     t.interrupt();
207 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
208 jsr166 1.15 }
209    
210     /**
211     * park is released by preceding interrupt
212     */
213     public void testParkAfterInterrupt() throws Exception {
214     final CountDownLatch threadStarted = new CountDownLatch(1);
215     final AtomicBoolean unparked = new AtomicBoolean(false);
216 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
217 jsr166 1.15 public void realRun() throws Exception {
218     threadStarted.countDown();
219     while (!unparked.get())
220     Thread.yield();
221     assertTrue(Thread.currentThread().isInterrupted());
222 jsr166 1.11 LockSupport.park();
223 jsr166 1.15 assertTrue(Thread.currentThread().isInterrupted());
224     }});
225    
226     threadStarted.await();
227     t.interrupt();
228     unparked.set(true);
229 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
230 jsr166 1.15 }
231    
232     /**
233     * parkUntil is released by preceding interrupt
234     */
235     public void testParkUntilAfterInterrupt() throws Exception {
236     final CountDownLatch threadStarted = new CountDownLatch(1);
237     final AtomicBoolean unparked = new AtomicBoolean(false);
238 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
239 jsr166 1.15 public void realRun() throws Exception {
240     threadStarted.countDown();
241     while (!unparked.get())
242     Thread.yield();
243     long d = new Date().getTime() + LONG_DELAY_MS;
244     long nanos = LONG_DELAY_MS * 1000L * 1000L;
245     long t0 = System.nanoTime();
246     LockSupport.parkUntil(d);
247     assertTrue(System.nanoTime() - t0 < nanos);
248     }});
249    
250     threadStarted.await();
251     t.interrupt();
252     unparked.set(true);
253 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
254 jsr166 1.15 }
255    
256     /**
257     * parkNanos is released by preceding interrupt
258     */
259     public void testParkNanosAfterInterrupt() throws Exception {
260     final CountDownLatch threadStarted = new CountDownLatch(1);
261     final AtomicBoolean unparked = new AtomicBoolean(false);
262 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
263 jsr166 1.15 public void realRun() throws Exception {
264     threadStarted.countDown();
265     while (!unparked.get())
266     Thread.yield();
267     long nanos = LONG_DELAY_MS * 1000L * 1000L;
268     long t0 = System.nanoTime();
269     LockSupport.parkNanos(nanos);
270     assertTrue(System.nanoTime() - t0 < nanos);
271 jsr166 1.11 }});
272    
273 jsr166 1.15 threadStarted.await();
274 jsr166 1.11 t.interrupt();
275 jsr166 1.15 unparked.set(true);
276 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
277 dl 1.4 }
278    
279     /**
280     * parkNanos times out if not unparked
281 dl 1.3 */
282 jsr166 1.15 public void testParkNanosTimesOut() throws InterruptedException {
283 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
284 jsr166 1.12 public void realRun() {
285 jsr166 1.15 final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L;
286     long t0 = System.nanoTime();
287     LockSupport.parkNanos(timeoutNanos);
288     assertTrue(System.nanoTime() - t0 >= timeoutNanos);
289 jsr166 1.11 }});
290    
291 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
292 dl 1.1 }
293    
294    
295 dl 1.3 /**
296 dl 1.4 * parkUntil times out if not unparked
297 dl 1.3 */
298 jsr166 1.15 public void testParkUntilTimesOut() throws InterruptedException {
299 jsr166 1.16 Thread t = newStartedThread(new CheckedRunnable() {
300 jsr166 1.12 public void realRun() {
301 jsr166 1.15 long d = new Date().getTime() + SHORT_DELAY_MS;
302     // beware of rounding
303     long timeoutNanos = (SHORT_DELAY_MS - 1) * 1000L * 1000L;
304     long t0 = System.nanoTime();
305 jsr166 1.11 LockSupport.parkUntil(d);
306 jsr166 1.15 assertTrue(System.nanoTime() - t0 >= timeoutNanos);
307     }});
308    
309 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
310 jsr166 1.15 }
311    
312     /**
313     * parkUntil(0) returns immediately
314     * Requires hotspot fix for:
315     * 6763959 java.util.concurrent.locks.LockSupport.parkUntil(0) blocks forever
316     */
317 jsr166 1.16 public void XXXXtestParkUntil0Returns() throws InterruptedException {
318     Thread t = newStartedThread(new CheckedRunnable() {
319 jsr166 1.15 public void realRun() {
320     LockSupport.parkUntil(0L);
321 jsr166 1.11 }});
322    
323 jsr166 1.16 awaitTermination(t, MEDIUM_DELAY_MS);
324 dl 1.1 }
325     }