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. |
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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
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); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
23 |
< |
* park is released by unpark occuring after park |
23 |
> |
* park is released by unpark occurring after park |
24 |
|
*/ |
25 |
< |
public void testPark() { |
25 |
> |
public void testPark() { |
26 |
|
Thread t = new Thread(new Runnable() { |
27 |
|
public void run() { |
28 |
|
try { |
29 |
|
LockSupport.park(); |
30 |
< |
} catch(Exception e){ |
30 |
> |
} catch (Exception e) { |
31 |
|
threadUnexpectedException(); |
32 |
|
} |
33 |
|
} |
38 |
|
LockSupport.unpark(t); |
39 |
|
t.join(); |
40 |
|
} |
41 |
< |
catch(Exception e) { |
41 |
> |
catch (Exception e) { |
42 |
|
unexpectedException(); |
43 |
|
} |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* park is released by unpark occuring before park |
47 |
> |
* park is released by unpark occurring before park |
48 |
|
*/ |
49 |
< |
public void testPark2() { |
49 |
> |
public void testPark2() { |
50 |
|
Thread t = new Thread(new Runnable() { |
51 |
|
public void run() { |
52 |
|
try { |
53 |
|
Thread.sleep(SHORT_DELAY_MS); |
54 |
|
LockSupport.park(); |
55 |
< |
} catch(Exception e){ |
55 |
> |
} catch (Exception e) { |
56 |
|
threadUnexpectedException(); |
57 |
|
} |
58 |
|
} |
62 |
|
LockSupport.unpark(t); |
63 |
|
t.join(); |
64 |
|
} |
65 |
< |
catch(Exception e) { |
65 |
> |
catch (Exception e) { |
66 |
|
unexpectedException(); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* park is released by interrupt |
71 |
> |
* park is released by interrupt |
72 |
|
*/ |
73 |
< |
public void testPark3() { |
73 |
> |
public void testPark3() { |
74 |
|
Thread t = new Thread(new Runnable() { |
75 |
|
public void run() { |
76 |
|
try { |
77 |
|
LockSupport.park(); |
78 |
< |
threadAssertTrue(Thread.interrupted()); |
78 |
< |
} catch(Exception e){ |
78 |
> |
} catch (Exception e) { |
79 |
|
threadUnexpectedException(); |
80 |
|
} |
81 |
|
} |
86 |
|
t.interrupt(); |
87 |
|
t.join(); |
88 |
|
} |
89 |
< |
catch(Exception e) { |
89 |
> |
catch (Exception e) { |
90 |
|
unexpectedException(); |
91 |
|
} |
92 |
|
} |
94 |
|
/** |
95 |
|
* park returns if interrupted before park |
96 |
|
*/ |
97 |
< |
public void testPark4() { |
97 |
> |
public void testPark4() { |
98 |
|
final ReentrantLock lock = new ReentrantLock(); |
99 |
|
lock.lock(); |
100 |
|
Thread t = new Thread(new Runnable() { |
102 |
|
try { |
103 |
|
lock.lock(); |
104 |
|
LockSupport.park(); |
105 |
< |
} catch(Exception e){ |
105 |
> |
} catch (Exception e) { |
106 |
|
threadUnexpectedException(); |
107 |
|
} |
108 |
|
} |
113 |
|
lock.unlock(); |
114 |
|
t.join(); |
115 |
|
} |
116 |
< |
catch(Exception e) { |
116 |
> |
catch (Exception e) { |
117 |
|
unexpectedException(); |
118 |
|
} |
119 |
|
} |
121 |
|
/** |
122 |
|
* parkNanos times out if not unparked |
123 |
|
*/ |
124 |
< |
public void testParkNanos() { |
124 |
> |
public void testParkNanos() { |
125 |
|
Thread t = new Thread(new Runnable() { |
126 |
|
public void run() { |
127 |
|
try { |
128 |
|
LockSupport.parkNanos(1000); |
129 |
< |
} catch(Exception e){ |
129 |
> |
} catch (Exception e) { |
130 |
|
threadUnexpectedException(); |
131 |
|
} |
132 |
|
} |
135 |
|
t.start(); |
136 |
|
t.join(); |
137 |
|
} |
138 |
< |
catch(Exception e) { |
138 |
> |
catch (Exception e) { |
139 |
|
unexpectedException(); |
140 |
|
} |
141 |
|
} |
144 |
|
/** |
145 |
|
* parkUntil times out if not unparked |
146 |
|
*/ |
147 |
< |
public void testParkUntil() { |
147 |
> |
public void testParkUntil() { |
148 |
|
Thread t = new Thread(new Runnable() { |
149 |
|
public void run() { |
150 |
|
try { |
151 |
|
long d = new Date().getTime() + 100; |
152 |
|
LockSupport.parkUntil(d); |
153 |
< |
} catch(Exception e){ |
153 |
> |
} catch (Exception e) { |
154 |
|
threadUnexpectedException(); |
155 |
|
} |
156 |
|
} |
159 |
|
t.start(); |
160 |
|
t.join(); |
161 |
|
} |
162 |
< |
catch(Exception e) { |
162 |
> |
catch (Exception e) { |
163 |
|
unexpectedException(); |
164 |
|
} |
165 |
|
} |