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 |
jsr166 |
1.11 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
dl |
1.1 |
|
14 |
dl |
1.2 |
public class ExchangerTest extends JSR166TestCase { |
15 |
jsr166 |
1.6 |
|
16 |
dl |
1.1 |
public static void main(String[] args) { |
17 |
jsr166 |
1.10 |
junit.textui.TestRunner.run (suite()); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
jsr166 |
1.10 |
return new TestSuite(ExchangerTest.class); |
21 |
dl |
1.1 |
} |
22 |
|
|
|
23 |
dl |
1.3 |
/** |
24 |
dl |
1.4 |
* exchange exchanges objects across two threads |
25 |
dl |
1.3 |
*/ |
26 |
jsr166 |
1.9 |
public void testExchange() throws InterruptedException { |
27 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
28 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedRunnable() { |
29 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
30 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
31 |
|
|
assertSame(two, e.exchange(one)); |
32 |
jsr166 |
1.9 |
}}); |
33 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
34 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
35 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
36 |
|
|
assertSame(one, e.exchange(two)); |
37 |
jsr166 |
1.9 |
}}); |
38 |
|
|
|
39 |
|
|
t1.start(); |
40 |
|
|
t2.start(); |
41 |
|
|
t1.join(); |
42 |
|
|
t2.join(); |
43 |
dl |
1.2 |
} |
44 |
|
|
|
45 |
dl |
1.3 |
/** |
46 |
dl |
1.4 |
* timed exchange exchanges objects across two threads |
47 |
dl |
1.3 |
*/ |
48 |
jsr166 |
1.9 |
public void testTimedExchange() throws InterruptedException { |
49 |
dl |
1.2 |
final Exchanger e = new Exchanger(); |
50 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedRunnable() { |
51 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
52 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
53 |
|
|
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
54 |
jsr166 |
1.9 |
}}); |
55 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
56 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
57 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one, SHORT_DELAY_MS, MILLISECONDS)); |
58 |
|
|
assertSame(one, e.exchange(two, SHORT_DELAY_MS, MILLISECONDS)); |
59 |
jsr166 |
1.9 |
}}); |
60 |
|
|
|
61 |
|
|
t1.start(); |
62 |
|
|
t2.start(); |
63 |
|
|
t1.join(); |
64 |
|
|
t2.join(); |
65 |
dl |
1.1 |
} |
66 |
|
|
|
67 |
dl |
1.3 |
/** |
68 |
dl |
1.4 |
* interrupt during wait for exchange throws IE |
69 |
dl |
1.3 |
*/ |
70 |
jsr166 |
1.9 |
public void testExchange_InterruptedException() throws InterruptedException { |
71 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
72 |
jsr166 |
1.10 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
73 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
74 |
|
|
e.exchange(one); |
75 |
|
|
}}); |
76 |
|
|
|
77 |
|
|
t.start(); |
78 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
79 |
|
|
t.interrupt(); |
80 |
|
|
t.join(); |
81 |
dl |
1.1 |
} |
82 |
|
|
|
83 |
dl |
1.3 |
/** |
84 |
dl |
1.4 |
* interrupt during wait for timed exchange throws IE |
85 |
dl |
1.3 |
*/ |
86 |
jsr166 |
1.9 |
public void testTimedExchange_InterruptedException() throws InterruptedException { |
87 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
88 |
jsr166 |
1.10 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
89 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
90 |
jsr166 |
1.13 |
e.exchange(null, SMALL_DELAY_MS, MILLISECONDS); |
91 |
jsr166 |
1.9 |
}}); |
92 |
|
|
|
93 |
|
|
t.start(); |
94 |
jsr166 |
1.12 |
Thread.sleep(SHORT_DELAY_MS); |
95 |
jsr166 |
1.9 |
t.interrupt(); |
96 |
|
|
t.join(); |
97 |
dl |
1.1 |
} |
98 |
|
|
|
99 |
dl |
1.3 |
/** |
100 |
dl |
1.4 |
* timeout during wait for timed exchange throws TOE |
101 |
dl |
1.3 |
*/ |
102 |
jsr166 |
1.9 |
public void testExchange_TimeOutException() throws InterruptedException { |
103 |
dl |
1.1 |
final Exchanger e = new Exchanger(); |
104 |
jsr166 |
1.13 |
Thread t = new ThreadShouldThrow(TimeoutException.class) { |
105 |
jsr166 |
1.9 |
public void realRun() throws Exception { |
106 |
jsr166 |
1.13 |
e.exchange(null, SHORT_DELAY_MS, MILLISECONDS); |
107 |
|
|
}}; |
108 |
jsr166 |
1.9 |
|
109 |
|
|
t.start(); |
110 |
|
|
t.join(); |
111 |
dl |
1.1 |
} |
112 |
dl |
1.4 |
|
113 |
|
|
/** |
114 |
|
|
* If one exchanging thread is interrupted, another succeeds. |
115 |
|
|
*/ |
116 |
jsr166 |
1.9 |
public void testReplacementAfterExchange() throws InterruptedException { |
117 |
dl |
1.4 |
final Exchanger e = new Exchanger(); |
118 |
jsr166 |
1.10 |
Thread t1 = new Thread(new CheckedInterruptedRunnable() { |
119 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
120 |
jsr166 |
1.13 |
assertSame(two, e.exchange(one)); |
121 |
|
|
e.exchange(two); |
122 |
jsr166 |
1.9 |
}}); |
123 |
jsr166 |
1.10 |
Thread t2 = new Thread(new CheckedRunnable() { |
124 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
125 |
jsr166 |
1.13 |
assertSame(one, e.exchange(two)); |
126 |
jsr166 |
1.9 |
Thread.sleep(SMALL_DELAY_MS); |
127 |
jsr166 |
1.13 |
assertSame(three, e.exchange(one)); |
128 |
jsr166 |
1.9 |
}}); |
129 |
jsr166 |
1.10 |
Thread t3 = new Thread(new CheckedRunnable() { |
130 |
jsr166 |
1.9 |
public void realRun() throws InterruptedException { |
131 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
132 |
jsr166 |
1.13 |
assertSame(one, e.exchange(three)); |
133 |
jsr166 |
1.9 |
}}); |
134 |
|
|
|
135 |
|
|
t1.start(); |
136 |
|
|
t2.start(); |
137 |
|
|
t3.start(); |
138 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
139 |
|
|
t1.interrupt(); |
140 |
|
|
t1.join(); |
141 |
|
|
t2.join(); |
142 |
|
|
t3.join(); |
143 |
dl |
1.4 |
} |
144 |
|
|
|
145 |
dl |
1.1 |
} |