ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalTest.java
Revision: 1.18
Committed: Tue Jan 26 13:33:06 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.17: +7 -7 lines
Log Message:
Replace Integer with Item class

File Contents

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