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.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
+ |
|
14 |
+ |
public class ExchangerTest extends JSR166TestCase { |
15 |
|
|
12 |
– |
public class ExchangerTest extends TestCase{ |
13 |
– |
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(ExchangerTest.class); |
20 |
> |
return new TestSuite(ExchangerTest.class); |
21 |
> |
} |
22 |
> |
|
23 |
> |
/** |
24 |
> |
* exchange exchanges objects across two threads |
25 |
> |
*/ |
26 |
> |
public void testExchange() throws InterruptedException { |
27 |
> |
final Exchanger e = new Exchanger(); |
28 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
29 |
> |
public void realRun() throws InterruptedException { |
30 |
> |
Object v = e.exchange(one); |
31 |
> |
threadAssertEquals(v, two); |
32 |
> |
Object w = e.exchange(v); |
33 |
> |
threadAssertEquals(w, one); |
34 |
> |
}}); |
35 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
36 |
> |
public void realRun() throws InterruptedException { |
37 |
> |
Object v = e.exchange(two); |
38 |
> |
threadAssertEquals(v, one); |
39 |
> |
Object w = e.exchange(v); |
40 |
> |
threadAssertEquals(w, two); |
41 |
> |
}}); |
42 |
> |
|
43 |
> |
t1.start(); |
44 |
> |
t2.start(); |
45 |
> |
t1.join(); |
46 |
> |
t2.join(); |
47 |
> |
} |
48 |
> |
|
49 |
> |
/** |
50 |
> |
* timed exchange exchanges objects across two threads |
51 |
> |
*/ |
52 |
> |
public void testTimedExchange() throws InterruptedException { |
53 |
> |
final Exchanger e = new Exchanger(); |
54 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
55 |
> |
public void realRun() throws Exception { |
56 |
> |
Object v = e.exchange(one, SHORT_DELAY_MS, MILLISECONDS); |
57 |
> |
threadAssertEquals(v, two); |
58 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS); |
59 |
> |
threadAssertEquals(w, one); |
60 |
> |
}}); |
61 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
62 |
> |
public void realRun() throws Exception { |
63 |
> |
Object v = e.exchange(two, SHORT_DELAY_MS, MILLISECONDS); |
64 |
> |
threadAssertEquals(v, one); |
65 |
> |
Object w = e.exchange(v, SHORT_DELAY_MS, MILLISECONDS); |
66 |
> |
threadAssertEquals(w, two); |
67 |
> |
}}); |
68 |
> |
|
69 |
> |
t1.start(); |
70 |
> |
t2.start(); |
71 |
> |
t1.join(); |
72 |
> |
t2.join(); |
73 |
> |
} |
74 |
> |
|
75 |
> |
/** |
76 |
> |
* interrupt during wait for exchange throws IE |
77 |
> |
*/ |
78 |
> |
public void testExchange_InterruptedException() throws InterruptedException { |
79 |
> |
final Exchanger e = new Exchanger(); |
80 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
81 |
> |
public void realRun() throws InterruptedException { |
82 |
> |
e.exchange(one); |
83 |
> |
}}); |
84 |
> |
|
85 |
> |
t.start(); |
86 |
> |
Thread.sleep(SHORT_DELAY_MS); |
87 |
> |
t.interrupt(); |
88 |
> |
t.join(); |
89 |
|
} |
90 |
|
|
91 |
< |
private static long SHORT_DELAY_MS = 100; |
92 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
93 |
< |
private static long LONG_DELAY_MS = 10000; |
94 |
< |
|
95 |
< |
private final static Integer one = new Integer(1); |
96 |
< |
private final static Integer two = new Integer(2); |
97 |
< |
|
98 |
< |
public void testExchange(){ |
99 |
< |
final Exchanger e = new Exchanger(); |
100 |
< |
Thread t1 = new Thread(new Runnable(){ |
101 |
< |
public void run(){ |
102 |
< |
try{ |
103 |
< |
Object v = e.exchange(one); |
104 |
< |
assertEquals(v, two); |
105 |
< |
Object w = e.exchange(v); |
106 |
< |
assertEquals(w, one); |
107 |
< |
}catch(InterruptedException e){ |
108 |
< |
fail("unexpected exception"); |
109 |
< |
} |
110 |
< |
} |
111 |
< |
}); |
112 |
< |
Thread t2 = new Thread(new Runnable(){ |
113 |
< |
public void run(){ |
114 |
< |
try{ |
115 |
< |
Object v = e.exchange(two); |
116 |
< |
assertEquals(v, one); |
117 |
< |
Object w = e.exchange(v); |
118 |
< |
assertEquals(w, two); |
119 |
< |
}catch(InterruptedException e){ |
120 |
< |
fail("unexpected exception"); |
121 |
< |
} |
122 |
< |
} |
123 |
< |
}); |
124 |
< |
try { |
125 |
< |
t1.start(); |
126 |
< |
t2.start(); |
127 |
< |
t1.join(); |
128 |
< |
t2.join(); |
129 |
< |
} catch(InterruptedException ex) { |
130 |
< |
fail("unexpected exception"); |
131 |
< |
} |
132 |
< |
} |
133 |
< |
|
134 |
< |
public void testExchange1_InterruptedException(){ |
135 |
< |
final Exchanger e = new Exchanger(); |
136 |
< |
Thread t = new Thread(new Runnable() { |
137 |
< |
public void run(){ |
138 |
< |
try{ |
139 |
< |
e.exchange(one); |
140 |
< |
fail("should throw"); |
141 |
< |
}catch(InterruptedException success){ |
142 |
< |
} |
143 |
< |
} |
144 |
< |
}); |
145 |
< |
try{ |
146 |
< |
t.start(); |
147 |
< |
Thread.sleep(SHORT_DELAY_MS); |
148 |
< |
t.interrupt(); |
149 |
< |
t.join(); |
150 |
< |
} catch(InterruptedException ex) { |
151 |
< |
fail("unexpected exception"); |
152 |
< |
} |
153 |
< |
} |
154 |
< |
|
155 |
< |
public void testExchange2_InterruptedException(){ |
156 |
< |
final Exchanger e = new Exchanger(); |
87 |
< |
Thread t = new Thread(new Runnable() { |
88 |
< |
public void run(){ |
89 |
< |
try{ |
90 |
< |
e.exchange(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
91 |
< |
fail("should throw"); |
92 |
< |
} catch(InterruptedException success){ |
93 |
< |
} catch(Exception e2){ |
94 |
< |
fail("should throw IE"); |
95 |
< |
} |
96 |
< |
} |
97 |
< |
}); |
98 |
< |
try{ |
99 |
< |
t.start(); |
100 |
< |
t.interrupt(); |
101 |
< |
t.join(); |
102 |
< |
} catch(InterruptedException ex){ |
103 |
< |
fail("unexpected exception"); |
104 |
< |
} |
105 |
< |
} |
106 |
< |
|
107 |
< |
public void testExchange3_TimeOutException(){ |
108 |
< |
final Exchanger e = new Exchanger(); |
109 |
< |
Thread t = new Thread(new Runnable() { |
110 |
< |
public void run(){ |
111 |
< |
try{ |
112 |
< |
e.exchange(null, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
113 |
< |
fail("should throw"); |
114 |
< |
}catch(TimeoutException success){} |
115 |
< |
catch(InterruptedException e2){ |
116 |
< |
fail("should throw TOE"); |
117 |
< |
} |
118 |
< |
} |
119 |
< |
}); |
120 |
< |
try{ |
121 |
< |
t.start(); |
122 |
< |
t.join(); |
123 |
< |
} catch(InterruptedException ex){ |
124 |
< |
fail("unexpected exception"); |
125 |
< |
} |
91 |
> |
/** |
92 |
> |
* interrupt during wait for timed exchange throws IE |
93 |
> |
*/ |
94 |
> |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
95 |
> |
final Exchanger e = new Exchanger(); |
96 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
97 |
> |
public void realRun() throws Exception { |
98 |
> |
e.exchange(null, MEDIUM_DELAY_MS, MILLISECONDS); |
99 |
> |
}}); |
100 |
> |
|
101 |
> |
t.start(); |
102 |
> |
t.interrupt(); |
103 |
> |
t.join(); |
104 |
> |
} |
105 |
> |
|
106 |
> |
/** |
107 |
> |
* timeout during wait for timed exchange throws TOE |
108 |
> |
*/ |
109 |
> |
public void testExchange_TimeOutException() throws InterruptedException { |
110 |
> |
final Exchanger e = new Exchanger(); |
111 |
> |
Thread t = new Thread(new CheckedRunnable() { |
112 |
> |
public void realRun() throws Exception { |
113 |
> |
try { |
114 |
> |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
115 |
> |
threadShouldThrow(); |
116 |
> |
} catch (TimeoutException success) {} |
117 |
> |
}}); |
118 |
> |
|
119 |
> |
t.start(); |
120 |
> |
t.join(); |
121 |
> |
} |
122 |
> |
|
123 |
> |
/** |
124 |
> |
* If one exchanging thread is interrupted, another succeeds. |
125 |
> |
*/ |
126 |
> |
public void testReplacementAfterExchange() throws InterruptedException { |
127 |
> |
final Exchanger e = new Exchanger(); |
128 |
> |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
129 |
> |
public void realRun() throws InterruptedException { |
130 |
> |
Object v = e.exchange(one); |
131 |
> |
threadAssertEquals(v, two); |
132 |
> |
Object w = e.exchange(v); |
133 |
> |
}}); |
134 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
135 |
> |
public void realRun() throws InterruptedException { |
136 |
> |
Object v = e.exchange(two); |
137 |
> |
threadAssertEquals(v, one); |
138 |
> |
Thread.sleep(SMALL_DELAY_MS); |
139 |
> |
Object w = e.exchange(v); |
140 |
> |
threadAssertEquals(w, three); |
141 |
> |
}}); |
142 |
> |
Thread t3 = new Thread(new CheckedRunnable() { |
143 |
> |
public void realRun() throws InterruptedException { |
144 |
> |
Thread.sleep(SMALL_DELAY_MS); |
145 |
> |
Object w = e.exchange(three); |
146 |
> |
threadAssertEquals(w, one); |
147 |
> |
}}); |
148 |
> |
|
149 |
> |
t1.start(); |
150 |
> |
t2.start(); |
151 |
> |
t3.start(); |
152 |
> |
Thread.sleep(SHORT_DELAY_MS); |
153 |
> |
t1.interrupt(); |
154 |
> |
t1.join(); |
155 |
> |
t2.join(); |
156 |
> |
t3.join(); |
157 |
|
} |
158 |
+ |
|
159 |
|
} |