1 |
dl |
1.1 |
/* |
2 |
dl |
1.5 |
* 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 |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
|
|
import java.util.concurrent.locks.*; |
13 |
|
|
|
14 |
jsr166 |
1.10 |
public class LockSupportTest extends JSR166TestCase { |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.12 |
junit.textui.TestRunner.run (suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
jsr166 |
1.12 |
return new TestSuite(LockSupportTest.class); |
20 |
dl |
1.1 |
} |
21 |
|
|
|
22 |
dl |
1.3 |
/** |
23 |
dl |
1.6 |
* park is released by unpark occurring after park |
24 |
dl |
1.3 |
*/ |
25 |
jsr166 |
1.11 |
public void testPark() throws InterruptedException { |
26 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
27 |
|
|
public void realRun() { |
28 |
jsr166 |
1.11 |
LockSupport.park(); |
29 |
|
|
}}); |
30 |
|
|
|
31 |
|
|
t.start(); |
32 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
33 |
|
|
LockSupport.unpark(t); |
34 |
|
|
t.join(); |
35 |
dl |
1.1 |
} |
36 |
|
|
|
37 |
dl |
1.3 |
/** |
38 |
dl |
1.6 |
* park is released by unpark occurring before park |
39 |
dl |
1.4 |
*/ |
40 |
jsr166 |
1.11 |
public void testPark2() throws InterruptedException { |
41 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
42 |
|
|
public void realRun() throws InterruptedException { |
43 |
jsr166 |
1.11 |
Thread.sleep(SHORT_DELAY_MS); |
44 |
|
|
LockSupport.park(); |
45 |
|
|
}}); |
46 |
|
|
|
47 |
|
|
t.start(); |
48 |
|
|
LockSupport.unpark(t); |
49 |
|
|
t.join(); |
50 |
dl |
1.4 |
} |
51 |
|
|
|
52 |
|
|
/** |
53 |
jsr166 |
1.8 |
* park is released by interrupt |
54 |
dl |
1.4 |
*/ |
55 |
jsr166 |
1.11 |
public void testPark3() throws InterruptedException { |
56 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
57 |
|
|
public void realRun() { |
58 |
jsr166 |
1.11 |
LockSupport.park(); |
59 |
|
|
}}); |
60 |
|
|
|
61 |
|
|
t.start(); |
62 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
63 |
|
|
t.interrupt(); |
64 |
|
|
t.join(); |
65 |
dl |
1.4 |
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* park returns if interrupted before park |
69 |
|
|
*/ |
70 |
jsr166 |
1.11 |
public void testPark4() throws InterruptedException { |
71 |
dl |
1.4 |
final ReentrantLock lock = new ReentrantLock(); |
72 |
|
|
lock.lock(); |
73 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
74 |
|
|
public void realRun() { |
75 |
jsr166 |
1.11 |
lock.lock(); |
76 |
|
|
LockSupport.park(); |
77 |
|
|
}}); |
78 |
|
|
|
79 |
|
|
t.start(); |
80 |
jsr166 |
1.13 |
Thread.sleep(SHORT_DELAY_MS); |
81 |
jsr166 |
1.11 |
t.interrupt(); |
82 |
|
|
lock.unlock(); |
83 |
|
|
t.join(); |
84 |
dl |
1.4 |
} |
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* parkNanos times out if not unparked |
88 |
dl |
1.3 |
*/ |
89 |
jsr166 |
1.11 |
public void testParkNanos() throws InterruptedException { |
90 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
91 |
|
|
public void realRun() { |
92 |
jsr166 |
1.11 |
LockSupport.parkNanos(1000); |
93 |
|
|
}}); |
94 |
|
|
|
95 |
|
|
t.start(); |
96 |
|
|
t.join(); |
97 |
dl |
1.1 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
dl |
1.3 |
/** |
101 |
dl |
1.4 |
* parkUntil times out if not unparked |
102 |
dl |
1.3 |
*/ |
103 |
jsr166 |
1.11 |
public void testParkUntil() throws InterruptedException { |
104 |
jsr166 |
1.12 |
Thread t = new Thread(new CheckedRunnable() { |
105 |
|
|
public void realRun() { |
106 |
jsr166 |
1.11 |
long d = new Date().getTime() + 100; |
107 |
|
|
LockSupport.parkUntil(d); |
108 |
|
|
}}); |
109 |
|
|
|
110 |
|
|
t.start(); |
111 |
|
|
t.join(); |
112 |
dl |
1.1 |
} |
113 |
|
|
} |