ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadLocalTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:56 2003 UTC vs.
Revision 1.14 by jsr166, Wed Dec 31 16:44:02 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines