1 |
dl |
1.1 |
/* |
2 |
dl |
1.4 |
* 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 |
jsr166 |
1.12 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
|
11 |
dl |
1.2 |
public class ThreadLocalTest extends JSR166TestCase { |
12 |
dl |
1.1 |
public static void main(String[] args) { |
13 |
jsr166 |
1.9 |
junit.textui.TestRunner.run(suite()); |
14 |
dl |
1.1 |
} |
15 |
jsr166 |
1.8 |
|
16 |
dl |
1.1 |
public static Test suite() { |
17 |
jsr166 |
1.9 |
return new TestSuite(ThreadLocalTest.class); |
18 |
dl |
1.1 |
} |
19 |
|
|
|
20 |
dl |
1.5 |
static ThreadLocal<Integer> tl = new ThreadLocal<Integer>() { |
21 |
|
|
public Integer initialValue() { |
22 |
|
|
return one; |
23 |
|
|
} |
24 |
|
|
}; |
25 |
|
|
|
26 |
|
|
static InheritableThreadLocal<Integer> itl = |
27 |
|
|
new InheritableThreadLocal<Integer>() { |
28 |
|
|
protected Integer initialValue() { |
29 |
|
|
return zero; |
30 |
|
|
} |
31 |
jsr166 |
1.8 |
|
32 |
dl |
1.5 |
protected Integer childValue(Integer parentValue) { |
33 |
|
|
return new Integer(parentValue.intValue() + 1); |
34 |
dl |
1.1 |
} |
35 |
|
|
}; |
36 |
|
|
|
37 |
dl |
1.3 |
/** |
38 |
|
|
* remove causes next access to return initial value |
39 |
|
|
*/ |
40 |
dl |
1.1 |
public void testRemove() { |
41 |
jsr166 |
1.11 |
assertSame(tl.get(), one); |
42 |
dl |
1.1 |
tl.set(two); |
43 |
jsr166 |
1.11 |
assertSame(tl.get(), two); |
44 |
dl |
1.1 |
tl.remove(); |
45 |
jsr166 |
1.11 |
assertSame(tl.get(), one); |
46 |
dl |
1.5 |
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* remove in InheritableThreadLocal causes next access to return |
50 |
|
|
* initial value |
51 |
|
|
*/ |
52 |
|
|
public void testRemoveITL() { |
53 |
jsr166 |
1.11 |
assertSame(itl.get(), zero); |
54 |
dl |
1.5 |
itl.set(two); |
55 |
jsr166 |
1.11 |
assertSame(itl.get(), two); |
56 |
dl |
1.5 |
itl.remove(); |
57 |
jsr166 |
1.11 |
assertSame(itl.get(), zero); |
58 |
dl |
1.5 |
} |
59 |
|
|
|
60 |
|
|
private class ITLThread extends Thread { |
61 |
|
|
final int[] x; |
62 |
|
|
ITLThread(int[] array) { x = array; } |
63 |
|
|
public void run() { |
64 |
|
|
Thread child = null; |
65 |
|
|
if (itl.get().intValue() < x.length - 1) { |
66 |
|
|
child = new ITLThread(x); |
67 |
|
|
child.start(); |
68 |
|
|
} |
69 |
jsr166 |
1.13 |
Thread.yield(); |
70 |
jsr166 |
1.8 |
|
71 |
dl |
1.5 |
int threadId = itl.get().intValue(); |
72 |
|
|
for (int j = 0; j < threadId; j++) { |
73 |
|
|
x[threadId]++; |
74 |
jsr166 |
1.13 |
Thread.yield(); |
75 |
dl |
1.5 |
} |
76 |
jsr166 |
1.8 |
|
77 |
dl |
1.5 |
if (child != null) { // Wait for child (if any) |
78 |
|
|
try { |
79 |
|
|
child.join(); |
80 |
jsr166 |
1.6 |
} catch (InterruptedException e) { |
81 |
jsr166 |
1.7 |
threadUnexpectedException(e); |
82 |
dl |
1.5 |
} |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* InheritableThreadLocal propagates generic values. |
89 |
|
|
*/ |
90 |
jsr166 |
1.10 |
public void testGenericITL() throws InterruptedException { |
91 |
dl |
1.5 |
final int threadCount = 10; |
92 |
|
|
final int x[] = new int[threadCount]; |
93 |
|
|
Thread progenitor = new ITLThread(x); |
94 |
jsr166 |
1.10 |
progenitor.start(); |
95 |
|
|
progenitor.join(); |
96 |
|
|
for (int i = 0; i < threadCount; i++) { |
97 |
|
|
assertEquals(i, x[i]); |
98 |
dl |
1.5 |
} |
99 |
dl |
1.1 |
} |
100 |
|
|
} |