11 |
|
import java.util.concurrent.*; |
12 |
|
import java.util.concurrent.locks.*; |
13 |
|
|
14 |
< |
public class LockSupportTest extends JSR166TestCase{ |
14 |
> |
public class LockSupportTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(LockSupportTest.class); |
19 |
> |
return new TestSuite(LockSupportTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
23 |
|
* park is released by unpark occurring after park |
24 |
|
*/ |
25 |
< |
public void testPark() { |
26 |
< |
Thread t = new Thread(new Runnable() { |
27 |
< |
public void run() { |
28 |
< |
try { |
29 |
< |
LockSupport.park(); |
30 |
< |
} catch(Exception e){ |
31 |
< |
threadUnexpectedException(); |
32 |
< |
} |
33 |
< |
} |
34 |
< |
}); |
35 |
< |
try { |
36 |
< |
t.start(); |
37 |
< |
Thread.sleep(SHORT_DELAY_MS); |
38 |
< |
LockSupport.unpark(t); |
39 |
< |
t.join(); |
40 |
< |
} |
41 |
< |
catch(Exception e) { |
42 |
< |
unexpectedException(); |
43 |
< |
} |
25 |
> |
public void testPark() throws InterruptedException { |
26 |
> |
Thread t = new Thread(new CheckedRunnable() { |
27 |
> |
public void realRun() { |
28 |
> |
LockSupport.park(); |
29 |
> |
}}); |
30 |
> |
|
31 |
> |
t.start(); |
32 |
> |
Thread.sleep(SHORT_DELAY_MS); |
33 |
> |
LockSupport.unpark(t); |
34 |
> |
t.join(); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* park is released by unpark occurring before park |
39 |
|
*/ |
40 |
< |
public void testPark2() { |
41 |
< |
Thread t = new Thread(new Runnable() { |
42 |
< |
public void run() { |
43 |
< |
try { |
44 |
< |
Thread.sleep(SHORT_DELAY_MS); |
45 |
< |
LockSupport.park(); |
46 |
< |
} catch(Exception e){ |
47 |
< |
threadUnexpectedException(); |
48 |
< |
} |
49 |
< |
} |
59 |
< |
}); |
60 |
< |
try { |
61 |
< |
t.start(); |
62 |
< |
LockSupport.unpark(t); |
63 |
< |
t.join(); |
64 |
< |
} |
65 |
< |
catch(Exception e) { |
66 |
< |
unexpectedException(); |
67 |
< |
} |
40 |
> |
public void testPark2() throws InterruptedException { |
41 |
> |
Thread t = new Thread(new CheckedRunnable() { |
42 |
> |
public void realRun() throws InterruptedException { |
43 |
> |
Thread.sleep(SHORT_DELAY_MS); |
44 |
> |
LockSupport.park(); |
45 |
> |
}}); |
46 |
> |
|
47 |
> |
t.start(); |
48 |
> |
LockSupport.unpark(t); |
49 |
> |
t.join(); |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
|
* park is released by interrupt |
54 |
|
*/ |
55 |
< |
public void testPark3() { |
56 |
< |
Thread t = new Thread(new Runnable() { |
57 |
< |
public void run() { |
58 |
< |
try { |
59 |
< |
LockSupport.park(); |
60 |
< |
} catch(Exception e){ |
61 |
< |
threadUnexpectedException(); |
62 |
< |
} |
63 |
< |
} |
64 |
< |
}); |
83 |
< |
try { |
84 |
< |
t.start(); |
85 |
< |
Thread.sleep(SHORT_DELAY_MS); |
86 |
< |
t.interrupt(); |
87 |
< |
t.join(); |
88 |
< |
} |
89 |
< |
catch(Exception e) { |
90 |
< |
unexpectedException(); |
91 |
< |
} |
55 |
> |
public void testPark3() throws InterruptedException { |
56 |
> |
Thread t = new Thread(new CheckedRunnable() { |
57 |
> |
public void realRun() { |
58 |
> |
LockSupport.park(); |
59 |
> |
}}); |
60 |
> |
|
61 |
> |
t.start(); |
62 |
> |
Thread.sleep(SHORT_DELAY_MS); |
63 |
> |
t.interrupt(); |
64 |
> |
t.join(); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
|
* park returns if interrupted before park |
69 |
|
*/ |
70 |
< |
public void testPark4() { |
70 |
> |
public void testPark4() throws InterruptedException { |
71 |
|
final ReentrantLock lock = new ReentrantLock(); |
72 |
|
lock.lock(); |
73 |
< |
Thread t = new Thread(new Runnable() { |
74 |
< |
public void run() { |
75 |
< |
try { |
76 |
< |
lock.lock(); |
77 |
< |
LockSupport.park(); |
78 |
< |
} catch(Exception e){ |
79 |
< |
threadUnexpectedException(); |
80 |
< |
} |
81 |
< |
} |
82 |
< |
}); |
83 |
< |
try { |
111 |
< |
t.start(); |
112 |
< |
t.interrupt(); |
113 |
< |
lock.unlock(); |
114 |
< |
t.join(); |
115 |
< |
} |
116 |
< |
catch(Exception e) { |
117 |
< |
unexpectedException(); |
118 |
< |
} |
73 |
> |
Thread t = new Thread(new CheckedRunnable() { |
74 |
> |
public void realRun() { |
75 |
> |
lock.lock(); |
76 |
> |
LockSupport.park(); |
77 |
> |
}}); |
78 |
> |
|
79 |
> |
t.start(); |
80 |
> |
Thread.sleep(SHORT_DELAY_MS); |
81 |
> |
t.interrupt(); |
82 |
> |
lock.unlock(); |
83 |
> |
t.join(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
|
* parkNanos times out if not unparked |
88 |
|
*/ |
89 |
< |
public void testParkNanos() { |
90 |
< |
Thread t = new Thread(new Runnable() { |
91 |
< |
public void run() { |
92 |
< |
try { |
93 |
< |
LockSupport.parkNanos(1000); |
94 |
< |
} catch(Exception e){ |
95 |
< |
threadUnexpectedException(); |
96 |
< |
} |
132 |
< |
} |
133 |
< |
}); |
134 |
< |
try { |
135 |
< |
t.start(); |
136 |
< |
t.join(); |
137 |
< |
} |
138 |
< |
catch(Exception e) { |
139 |
< |
unexpectedException(); |
140 |
< |
} |
89 |
> |
public void testParkNanos() throws InterruptedException { |
90 |
> |
Thread t = new Thread(new CheckedRunnable() { |
91 |
> |
public void realRun() { |
92 |
> |
LockSupport.parkNanos(1000); |
93 |
> |
}}); |
94 |
> |
|
95 |
> |
t.start(); |
96 |
> |
t.join(); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
/** |
101 |
|
* parkUntil times out if not unparked |
102 |
|
*/ |
103 |
< |
public void testParkUntil() { |
104 |
< |
Thread t = new Thread(new Runnable() { |
105 |
< |
public void run() { |
106 |
< |
try { |
107 |
< |
long d = new Date().getTime() + 100; |
108 |
< |
LockSupport.parkUntil(d); |
109 |
< |
} catch(Exception e){ |
110 |
< |
threadUnexpectedException(); |
111 |
< |
} |
156 |
< |
} |
157 |
< |
}); |
158 |
< |
try { |
159 |
< |
t.start(); |
160 |
< |
t.join(); |
161 |
< |
} |
162 |
< |
catch(Exception e) { |
163 |
< |
unexpectedException(); |
164 |
< |
} |
103 |
> |
public void testParkUntil() throws InterruptedException { |
104 |
> |
Thread t = new Thread(new CheckedRunnable() { |
105 |
> |
public void realRun() { |
106 |
> |
long d = new Date().getTime() + 100; |
107 |
> |
LockSupport.parkUntil(d); |
108 |
> |
}}); |
109 |
> |
|
110 |
> |
t.start(); |
111 |
> |
t.join(); |
112 |
|
} |
113 |
|
} |