1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.util.concurrent.locks.*; |
12 |
|
13 |
public class LockSupportTest extends JSR166TestCase{ |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(LockSupportTest.class); |
19 |
} |
20 |
|
21 |
/** |
22 |
* |
23 |
*/ |
24 |
public void testUnpark() { |
25 |
Thread t = new Thread(new Runnable() { |
26 |
public void run() { |
27 |
try { |
28 |
LockSupport.park(); |
29 |
} catch(Exception e){ |
30 |
threadUnexpectedException(); |
31 |
} |
32 |
} |
33 |
}); |
34 |
t.start(); |
35 |
try { |
36 |
LockSupport.unpark(t); |
37 |
t.join(); |
38 |
} |
39 |
catch(Exception e) { |
40 |
unexpectedException(); |
41 |
} |
42 |
} |
43 |
|
44 |
/** |
45 |
* |
46 |
*/ |
47 |
public void testParkNanos() { |
48 |
Thread t = new Thread(new Runnable() { |
49 |
public void run() { |
50 |
try { |
51 |
LockSupport.parkNanos(1000); |
52 |
} catch(Exception e){ |
53 |
threadUnexpectedException(); |
54 |
} |
55 |
} |
56 |
}); |
57 |
try { |
58 |
t.start(); |
59 |
t.join(); |
60 |
} |
61 |
catch(Exception e) { |
62 |
unexpectedException(); |
63 |
} |
64 |
} |
65 |
|
66 |
|
67 |
/** |
68 |
* |
69 |
*/ |
70 |
public void testParkUntil() { |
71 |
Thread t = new Thread(new Runnable() { |
72 |
public void run() { |
73 |
try { |
74 |
long d = new Date().getTime() + 100; |
75 |
LockSupport.parkUntil(d); |
76 |
} catch(Exception e){ |
77 |
threadUnexpectedException(); |
78 |
} |
79 |
} |
80 |
}); |
81 |
try { |
82 |
t.start(); |
83 |
t.join(); |
84 |
} |
85 |
catch(Exception e) { |
86 |
unexpectedException(); |
87 |
} |
88 |
} |
89 |
} |