22 |
|
/** |
23 |
|
* exchange exchanges objects across two threads |
24 |
|
*/ |
25 |
< |
public void testExchange() { |
25 |
> |
public void testExchange() throws InterruptedException { |
26 |
|
final Exchanger e = new Exchanger(); |
27 |
< |
Thread t1 = new Thread(new Runnable() { |
28 |
< |
public void run() { |
29 |
< |
try { |
30 |
< |
Object v = e.exchange(one); |
31 |
< |
threadAssertEquals(v, two); |
32 |
< |
Object w = e.exchange(v); |
33 |
< |
threadAssertEquals(w, one); |
34 |
< |
} catch (InterruptedException e) { |
35 |
< |
threadUnexpectedException(); |
36 |
< |
} |
37 |
< |
} |
38 |
< |
}); |
39 |
< |
Thread t2 = new Thread(new Runnable() { |
40 |
< |
public void run() { |
41 |
< |
try { |
42 |
< |
Object v = e.exchange(two); |
43 |
< |
threadAssertEquals(v, one); |
44 |
< |
Object w = e.exchange(v); |
45 |
< |
threadAssertEquals(w, two); |
46 |
< |
} catch (InterruptedException e) { |
47 |
< |
threadUnexpectedException(); |
48 |
< |
} |
49 |
< |
} |
50 |
< |
}); |
51 |
< |
try { |
52 |
< |
t1.start(); |
53 |
< |
t2.start(); |
54 |
< |
t1.join(); |
55 |
< |
t2.join(); |
56 |
< |
} catch (InterruptedException ex) { |
57 |
< |
unexpectedException(); |
58 |
< |
} |
27 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
28 |
> |
public void realRun() throws InterruptedException { |
29 |
> |
Object v = e.exchange(one); |
30 |
> |
threadAssertEquals(v, two); |
31 |
> |
Object w = e.exchange(v); |
32 |
> |
threadAssertEquals(w, one); |
33 |
> |
}}); |
34 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
35 |
> |
public void realRun() throws InterruptedException { |
36 |
> |
Object v = e.exchange(two); |
37 |
> |
threadAssertEquals(v, one); |
38 |
> |
Object w = e.exchange(v); |
39 |
> |
threadAssertEquals(w, two); |
40 |
> |
}}); |
41 |
> |
|
42 |
> |
t1.start(); |
43 |
> |
t2.start(); |
44 |
> |
t1.join(); |
45 |
> |
t2.join(); |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
|
* timed exchange exchanges objects across two threads |
50 |
|
*/ |
51 |
< |
public void testTimedExchange() { |
51 |
> |
public void testTimedExchange() throws InterruptedException { |
52 |
|
final Exchanger e = new Exchanger(); |
53 |
< |
Thread t1 = new Thread(new Runnable() { |
54 |
< |
public void run() { |
55 |
< |
try { |
56 |
< |
Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
57 |
< |
threadAssertEquals(v, two); |
58 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
59 |
< |
threadAssertEquals(w, one); |
60 |
< |
} catch (InterruptedException e) { |
61 |
< |
threadUnexpectedException(); |
62 |
< |
} catch (TimeoutException toe) { |
63 |
< |
threadUnexpectedException(); |
64 |
< |
} |
65 |
< |
} |
66 |
< |
}); |
67 |
< |
Thread t2 = new Thread(new Runnable() { |
68 |
< |
public void run() { |
69 |
< |
try { |
70 |
< |
Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
71 |
< |
threadAssertEquals(v, one); |
85 |
< |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
86 |
< |
threadAssertEquals(w, two); |
87 |
< |
} catch (InterruptedException e) { |
88 |
< |
threadUnexpectedException(); |
89 |
< |
} catch (TimeoutException toe) { |
90 |
< |
threadUnexpectedException(); |
91 |
< |
} |
92 |
< |
} |
93 |
< |
}); |
94 |
< |
try { |
95 |
< |
t1.start(); |
96 |
< |
t2.start(); |
97 |
< |
t1.join(); |
98 |
< |
t2.join(); |
99 |
< |
} catch (InterruptedException ex) { |
100 |
< |
unexpectedException(); |
101 |
< |
} |
53 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
54 |
> |
public void realRun() throws Exception { |
55 |
> |
Object v = e.exchange(one, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
56 |
> |
threadAssertEquals(v, two); |
57 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
58 |
> |
threadAssertEquals(w, one); |
59 |
> |
}}); |
60 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
61 |
> |
public void realRun() throws Exception { |
62 |
> |
Object v = e.exchange(two, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
63 |
> |
threadAssertEquals(v, one); |
64 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
65 |
> |
threadAssertEquals(w, two); |
66 |
> |
}}); |
67 |
> |
|
68 |
> |
t1.start(); |
69 |
> |
t2.start(); |
70 |
> |
t1.join(); |
71 |
> |
t2.join(); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
|
* interrupt during wait for exchange throws IE |
76 |
|
*/ |
77 |
< |
public void testExchange_InterruptedException() { |
77 |
> |
public void testExchange_InterruptedException() throws InterruptedException { |
78 |
|
final Exchanger e = new Exchanger(); |
79 |
< |
Thread t = new Thread(new Runnable() { |
80 |
< |
public void run() { |
81 |
< |
try { |
82 |
< |
e.exchange(one); |
83 |
< |
threadShouldThrow(); |
84 |
< |
} catch (InterruptedException success) { |
85 |
< |
} |
86 |
< |
} |
87 |
< |
}); |
118 |
< |
try { |
119 |
< |
t.start(); |
120 |
< |
Thread.sleep(SHORT_DELAY_MS); |
121 |
< |
t.interrupt(); |
122 |
< |
t.join(); |
123 |
< |
} catch (InterruptedException ex) { |
124 |
< |
unexpectedException(); |
125 |
< |
} |
79 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
80 |
> |
public void realRun() throws InterruptedException { |
81 |
> |
e.exchange(one); |
82 |
> |
}}); |
83 |
> |
|
84 |
> |
t.start(); |
85 |
> |
Thread.sleep(SHORT_DELAY_MS); |
86 |
> |
t.interrupt(); |
87 |
> |
t.join(); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* interrupt during wait for timed exchange throws IE |
92 |
|
*/ |
93 |
< |
public void testTimedExchange_InterruptedException() { |
93 |
> |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
94 |
|
final Exchanger e = new Exchanger(); |
95 |
< |
Thread t = new Thread(new Runnable() { |
96 |
< |
public void run() { |
97 |
< |
try { |
98 |
< |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
99 |
< |
threadShouldThrow(); |
100 |
< |
} catch (InterruptedException success) { |
101 |
< |
} catch (Exception e2) { |
102 |
< |
threadFail("should throw IE"); |
141 |
< |
} |
142 |
< |
} |
143 |
< |
}); |
144 |
< |
try { |
145 |
< |
t.start(); |
146 |
< |
t.interrupt(); |
147 |
< |
t.join(); |
148 |
< |
} catch (InterruptedException ex) { |
149 |
< |
unexpectedException(); |
150 |
< |
} |
95 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
96 |
> |
public void realRun() throws Exception { |
97 |
> |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
98 |
> |
}}); |
99 |
> |
|
100 |
> |
t.start(); |
101 |
> |
t.interrupt(); |
102 |
> |
t.join(); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
|
* timeout during wait for timed exchange throws TOE |
107 |
|
*/ |
108 |
< |
public void testExchange_TimeOutException() { |
108 |
> |
public void testExchange_TimeOutException() throws InterruptedException { |
109 |
|
final Exchanger e = new Exchanger(); |
110 |
< |
Thread t = new Thread(new Runnable() { |
111 |
< |
public void run() { |
112 |
< |
try { |
113 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
< |
threadShouldThrow(); |
115 |
< |
} catch (TimeoutException success) { |
116 |
< |
} catch (InterruptedException e2) { |
117 |
< |
threadFail("should throw TOE"); |
118 |
< |
} |
119 |
< |
} |
168 |
< |
}); |
169 |
< |
try { |
170 |
< |
t.start(); |
171 |
< |
t.join(); |
172 |
< |
} catch (InterruptedException ex) { |
173 |
< |
unexpectedException(); |
174 |
< |
} |
110 |
> |
Thread t = new Thread(new CheckedRunnable() { |
111 |
> |
public void realRun() throws Exception { |
112 |
> |
try { |
113 |
> |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
114 |
> |
threadShouldThrow(); |
115 |
> |
} catch (TimeoutException success) {} |
116 |
> |
}}); |
117 |
> |
|
118 |
> |
t.start(); |
119 |
> |
t.join(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
|
* If one exchanging thread is interrupted, another succeeds. |
124 |
|
*/ |
125 |
< |
public void testReplacementAfterExchange() { |
125 |
> |
public void testReplacementAfterExchange() throws InterruptedException { |
126 |
|
final Exchanger e = new Exchanger(); |
127 |
< |
Thread t1 = new Thread(new Runnable() { |
128 |
< |
public void run() { |
129 |
< |
try { |
130 |
< |
Object v = e.exchange(one); |
131 |
< |
threadAssertEquals(v, two); |
132 |
< |
Object w = e.exchange(v); |
133 |
< |
threadShouldThrow(); |
134 |
< |
} catch (InterruptedException success) { |
135 |
< |
} |
136 |
< |
} |
137 |
< |
}); |
138 |
< |
Thread t2 = new Thread(new Runnable() { |
139 |
< |
public void run() { |
140 |
< |
try { |
141 |
< |
Object v = e.exchange(two); |
142 |
< |
threadAssertEquals(v, one); |
143 |
< |
Thread.sleep(SMALL_DELAY_MS); |
144 |
< |
Object w = e.exchange(v); |
145 |
< |
threadAssertEquals(w, three); |
146 |
< |
} catch (InterruptedException e) { |
147 |
< |
threadUnexpectedException(); |
148 |
< |
} |
149 |
< |
} |
150 |
< |
}); |
151 |
< |
Thread t3 = new Thread(new Runnable() { |
152 |
< |
public void run() { |
153 |
< |
try { |
154 |
< |
Thread.sleep(SMALL_DELAY_MS); |
155 |
< |
Object w = e.exchange(three); |
211 |
< |
threadAssertEquals(w, one); |
212 |
< |
} catch (InterruptedException e) { |
213 |
< |
threadUnexpectedException(); |
214 |
< |
} |
215 |
< |
} |
216 |
< |
}); |
217 |
< |
|
218 |
< |
try { |
219 |
< |
t1.start(); |
220 |
< |
t2.start(); |
221 |
< |
t3.start(); |
222 |
< |
Thread.sleep(SHORT_DELAY_MS); |
223 |
< |
t1.interrupt(); |
224 |
< |
t1.join(); |
225 |
< |
t2.join(); |
226 |
< |
t3.join(); |
227 |
< |
} catch (InterruptedException ex) { |
228 |
< |
unexpectedException(); |
229 |
< |
} |
127 |
> |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
128 |
> |
public void realRun() throws InterruptedException { |
129 |
> |
Object v = e.exchange(one); |
130 |
> |
threadAssertEquals(v, two); |
131 |
> |
Object w = e.exchange(v); |
132 |
> |
}}); |
133 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
134 |
> |
public void realRun() throws InterruptedException { |
135 |
> |
Object v = e.exchange(two); |
136 |
> |
threadAssertEquals(v, one); |
137 |
> |
Thread.sleep(SMALL_DELAY_MS); |
138 |
> |
Object w = e.exchange(v); |
139 |
> |
threadAssertEquals(w, three); |
140 |
> |
}}); |
141 |
> |
Thread t3 = new Thread(new CheckedRunnable() { |
142 |
> |
public void realRun() throws InterruptedException { |
143 |
> |
Thread.sleep(SMALL_DELAY_MS); |
144 |
> |
Object w = e.exchange(three); |
145 |
> |
threadAssertEquals(w, one); |
146 |
> |
}}); |
147 |
> |
|
148 |
> |
t1.start(); |
149 |
> |
t2.start(); |
150 |
> |
t3.start(); |
151 |
> |
Thread.sleep(SHORT_DELAY_MS); |
152 |
> |
t1.interrupt(); |
153 |
> |
t1.join(); |
154 |
> |
t2.join(); |
155 |
> |
t3.join(); |
156 |
|
} |
157 |
|
|
158 |
|
} |