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.6 |
* 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 |
|
|
|
13 |
dl |
1.2 |
public class ExchangerTest extends JSR166TestCase { |
14 |
jsr166 |
1.6 |
|
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.10 |
junit.textui.TestRunner.run (suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
jsr166 |
1.10 |
return new TestSuite(ExchangerTest.class); |
20 |
dl |
1.1 |
} |
21 |
|
|
|
22 |
dl |
1.3 |
/** |
23 |
dl |
1.4 |
* exchange exchanges objects across two threads |
24 |
dl |
1.3 |
*/ |
25 |
jsr166 |
1.9 |
public void testExchange() throws InterruptedException { |
26 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
27 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedRunnable() { |
28 |
jsr166 |
1.9 |
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 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
35 |
jsr166 |
1.9 |
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 |
dl |
1.2 |
} |
47 |
|
|
|
48 |
dl |
1.3 |
/** |
49 |
dl |
1.4 |
* timed exchange exchanges objects across two threads |
50 |
dl |
1.3 |
*/ |
51 |
jsr166 |
1.9 |
public void testTimedExchange() throws InterruptedException { |
52 |
dl |
1.2 |
final Exchanger e = new Exchanger(); |
53 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedRunnable() { |
54 |
jsr166 |
1.9 |
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 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
61 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
} |
73 |
|
|
|
74 |
dl |
1.3 |
/** |
75 |
dl |
1.4 |
* interrupt during wait for exchange throws IE |
76 |
dl |
1.3 |
*/ |
77 |
jsr166 |
1.9 |
public void testExchange_InterruptedException() throws InterruptedException { |
78 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
79 |
jsr166 |
1.10 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
80 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
} |
89 |
|
|
|
90 |
dl |
1.3 |
/** |
91 |
dl |
1.4 |
* interrupt during wait for timed exchange throws IE |
92 |
dl |
1.3 |
*/ |
93 |
jsr166 |
1.9 |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
94 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
95 |
jsr166 |
1.10 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
96 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
} |
104 |
|
|
|
105 |
dl |
1.3 |
/** |
106 |
dl |
1.4 |
* timeout during wait for timed exchange throws TOE |
107 |
dl |
1.3 |
*/ |
108 |
jsr166 |
1.9 |
public void testExchange_TimeOutException() throws InterruptedException { |
109 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
110 |
jsr166 |
1.10 |
Thread t = new Thread(new CheckedRunnable() { |
111 |
jsr166 |
1.9 |
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 |
dl |
1.1 |
} |
121 |
dl |
1.4 |
|
122 |
|
|
/** |
123 |
|
|
* If one exchanging thread is interrupted, another succeeds. |
124 |
|
|
*/ |
125 |
jsr166 |
1.9 |
public void testReplacementAfterExchange() throws InterruptedException { |
126 |
dl |
1.4 |
final Exchanger e = new Exchanger(); |
127 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
128 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
129 |
|
|
Object v = e.exchange(one); |
130 |
|
|
threadAssertEquals(v, two); |
131 |
|
|
Object w = e.exchange(v); |
132 |
|
|
}}); |
133 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
134 |
jsr166 |
1.9 |
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 |
jsr166 |
1.10 |
Thread t3 = new Thread(new CheckedRunnable() { |
142 |
jsr166 |
1.9 |
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 |
dl |
1.4 |
} |
157 |
|
|
|
158 |
dl |
1.1 |
} |