--- jsr166/src/test/tck/ThreadLocalTest.java 2009/08/04 10:13:48 1.7 +++ jsr166/src/test/tck/ThreadLocalTest.java 2021/01/26 13:33:06 1.18 @@ -1,37 +1,37 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * http://creativecommons.org/publicdomain/zero/1.0/ + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.concurrent.Semaphore; +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadLocalTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } - + public static Test suite() { - return new TestSuite(ThreadLocalTest.class); + return new TestSuite(ThreadLocalTest.class); } - static ThreadLocal tl = new ThreadLocal() { - public Integer initialValue() { + static ThreadLocal tl = new ThreadLocal() { + public Item initialValue() { return one; } }; - static InheritableThreadLocal itl = - new InheritableThreadLocal() { - protected Integer initialValue() { + static InheritableThreadLocal itl = + new InheritableThreadLocal() { + protected Item initialValue() { return zero; } - - protected Integer childValue(Integer parentValue) { - return new Integer(parentValue.intValue() + 1); + + protected Item childValue(Item parentValue) { + return new Item(parentValue.intValue() + 1); } }; @@ -39,11 +39,11 @@ public class ThreadLocalTest extends JSR * remove causes next access to return initial value */ public void testRemove() { - assertEquals(tl.get(), one); + assertSame(tl.get(), one); tl.set(two); - assertEquals(tl.get(), two); + assertSame(tl.get(), two); tl.remove(); - assertEquals(tl.get(), one); + assertSame(tl.get(), one); } /** @@ -51,11 +51,11 @@ public class ThreadLocalTest extends JSR * initial value */ public void testRemoveITL() { - assertEquals(itl.get(), zero); + assertSame(itl.get(), zero); itl.set(two); - assertEquals(itl.get(), two); + assertSame(itl.get(), two); itl.remove(); - assertEquals(itl.get(), zero); + assertSame(itl.get(), zero); } private class ITLThread extends Thread { @@ -67,14 +67,14 @@ public class ThreadLocalTest extends JSR child = new ITLThread(x); child.start(); } - Thread.currentThread().yield(); - + Thread.yield(); + int threadId = itl.get().intValue(); for (int j = 0; j < threadId; j++) { x[threadId]++; - Thread.currentThread().yield(); + Thread.yield(); } - + if (child != null) { // Wait for child (if any) try { child.join(); @@ -88,18 +88,14 @@ public class ThreadLocalTest extends JSR /** * InheritableThreadLocal propagates generic values. */ - public void testGenericITL() { + public void testGenericITL() throws InterruptedException { final int threadCount = 10; - final int x[] = new int[threadCount]; + final int[] x = new int[threadCount]; Thread progenitor = new ITLThread(x); - try { - progenitor.start(); - progenitor.join(); - for (int i = 0; i < threadCount; i++) { - assertEquals(i, x[i]); - } - } catch (InterruptedException e) { - unexpectedException(e); + progenitor.start(); + progenitor.join(); + for (int i = 0; i < threadCount; i++) { + assertEquals(i, x[i]); } } }