Initial load
diff --git a/test/java/lang/ThreadLocal/Basic.java b/test/java/lang/ThreadLocal/Basic.java
new file mode 100644
index 0000000..90edc50
--- /dev/null
+++ b/test/java/lang/ThreadLocal/Basic.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @summary Basic functional test of ThreadLocal
+ * @author Josh Bloch
+ */
+
+public class Basic {
+ static ThreadLocal n = new ThreadLocal() {
+ int i = 0;
+ protected synchronized Object initialValue() {
+ return new Integer(i++);
+ }
+ };
+
+ public static void main(String args[]) throws Exception {
+ int threadCount = 100;
+ Thread th[] = new Thread[threadCount];
+ final int x[] = new int[threadCount];
+
+ // Start the threads
+ for(int i=0; i<threadCount; i++) {
+ th[i] = new Thread() {
+ public void run() {
+ int threadId = ((Integer)(n.get())).intValue();
+ for (int j=0; j<threadId; j++) {
+ x[threadId]++;
+ Thread.currentThread().yield();
+ }
+ }
+ };
+ th[i].start();
+ }
+
+ // Wait for the threads to finish
+ for(int i=0; i<threadCount; i++)
+ th[i].join();
+
+ // Check results
+ for(int i=0; i<threadCount; i++)
+ if (x[i] != i)
+ throw(new Exception("x[" + i + "] =" + x[i]));
+ }
+}
diff --git a/test/java/lang/ThreadLocal/ImmutableLocal.java b/test/java/lang/ThreadLocal/ImmutableLocal.java
new file mode 100644
index 0000000..df6c86d
--- /dev/null
+++ b/test/java/lang/ThreadLocal/ImmutableLocal.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6387919
+ * @summary Confirm ThreadLocal.set() usage is not a side effect of get()
+ * @author Pete Soper
+ */
+public class ImmutableLocal
+{
+ /**
+ * {@link ThreadLocal} guaranteed to always return the same reference.
+ */
+ abstract public static class ImmutableThreadLocal extends ThreadLocal {
+ public void set(final Object value) {
+ throw new RuntimeException("ImmutableThreadLocal set called");
+ }
+
+ // force override
+ abstract protected Object initialValue();
+ }
+
+ private static final ThreadLocal cache = new ImmutableThreadLocal() {
+ public Object initialValue() {
+ return Thread.currentThread().getName();
+ }
+ };
+
+ public static void main(final String[] args) {
+ System.out.println("cache.get() = " + cache.get());
+ }
+}
diff --git a/test/java/lang/ThreadLocal/InitialValue.java b/test/java/lang/ThreadLocal/InitialValue.java
new file mode 100644
index 0000000..61d5feb
--- /dev/null
+++ b/test/java/lang/ThreadLocal/InitialValue.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5025230
+ * @summary Tests to see that a set nested in initialValue works OK
+ * @author Pete Soper
+ */
+public class InitialValue implements Runnable {
+
+ static ThreadLocal<String> other;
+ static boolean passed;
+
+ public class MyLocal extends ThreadLocal<String> {
+ String val;
+ protected String initialValue() {
+ other = new ThreadLocal<String>();
+ // This should reuse the map that the containing get() created
+ // or visa versa (i.e. instead of a second map being created).
+ other.set("Other");
+ return "Initial";
+ }
+ }
+
+ public void run() {
+ MyLocal l = new MyLocal();
+ // This should pick up the initial value
+ String s1 = l.get();
+ // And this should pick up the other local in this thread's locals map
+ String s2 = other.get();
+ if ((s2 != null) && s2.equals("Other")) {
+ // JMM guarantees this will be visible to
+ // another thread joining with this thread's
+ // termination: no need for this to be volatile.
+ passed = true;
+ }
+ }
+
+ public static void main(String[] args) {
+ // Starting with Mustang the main thread already has an initialized
+ // ThreadLocal map at this point, so test with a second thread.
+ Thread t = new Thread(new InitialValue());
+ t.start();
+ try {
+ t.join();
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Test Interrupted: failed");
+ }
+ if (!passed) {
+ throw new RuntimeException("Test Failed");
+ }
+ }
+}
diff --git a/test/java/lang/ThreadLocal/MemoryLeak.java b/test/java/lang/ThreadLocal/MemoryLeak.java
new file mode 100644
index 0000000..8e90df5
--- /dev/null
+++ b/test/java/lang/ThreadLocal/MemoryLeak.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4414045
+ * @summary Tests to see that memory leak no longer exists.
+ * @author Josh Bloch
+ */
+
+public class MemoryLeak {
+ public static void main(String[] args) {
+ for (int i = 0; i < 1100000; i++) {
+ ThreadLocal t = new ThreadLocal();
+ t.set(new Object());
+ t.set(null);
+ }
+ }
+}
diff --git a/test/java/lang/ThreadLocal/TLRemoveTest.java b/test/java/lang/ThreadLocal/TLRemoveTest.java
new file mode 100644
index 0000000..46f3a8a
--- /dev/null
+++ b/test/java/lang/ThreadLocal/TLRemoveTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @summary Basic functional test of remove method for ThreadLocal
+ * @author Seetharama Avadhanam
+ */
+
+public class TLRemoveTest {
+ private static final int INITIAL_VALUE = 101;
+ private static final int REMOVE_SET_VALUE = 102;
+
+ static ThreadLocal<Integer> n = new ThreadLocal<Integer>() {
+ protected synchronized Integer initialValue() {
+ return INITIAL_VALUE;
+ }
+ };
+
+ public static void main(String args[]) throws Throwable {
+ int threadCount = 100;
+ final int[] removeNode = {10,20,45,38};
+ // ThreadLocal values will be removed for these threads.
+ final int[] removeAndSet = {12,34,10};
+ // ThreadLocal values will be removed and sets new values..
+
+ Thread th[] = new Thread[threadCount];
+ final int x[] = new int[threadCount];
+ final Throwable exceptions[] = new Throwable[threadCount];
+
+ for(int i = 0; i<threadCount; i++) {
+ final int threadId = i;
+ th[i] = new Thread() {
+ public void run() {
+ try{
+ n.set(threadId); // Sets threadId as threadlocal value...
+ for (int j = 0; j<threadId; j++)
+ Thread.currentThread().yield();
+
+ // To remove the ThreadLocal ....
+ for(int removeId : removeNode)
+ if(threadId == removeId){
+ n.remove(); // Removes ThreadLocal values..
+ break;
+ }
+
+ // To remove the ThreadLocal value and set new value ...
+ for(int removeId : removeAndSet)
+ if(threadId == removeId){
+ n.remove(); // Removes the ThreadLocal Value...
+ n.set(REMOVE_SET_VALUE); /* Setting new Values to
+ ThreadLocal */
+ break;
+ }
+ /* Storing the threadlocal values in 'x'
+ ...so that it can be used for checking results... */
+ x[threadId] = n.get();
+ }
+ catch(Throwable ex){
+ exceptions[threadId] = ex;
+ }
+ }
+ };
+ th[i].start();
+ }
+
+ // Wait for the threads to finish
+ for(int i = 0; i<threadCount; i++)
+ th[i].join();
+
+ // Check results
+ for(int i = 0; i<threadCount; i++){
+ int checkValue = i;
+
+ /* If the remove method is called then the ThreadLocal value will
+ * be its initial value */
+ for(int removeId : removeNode)
+ if(removeId == i){
+ checkValue = INITIAL_VALUE;
+ break;
+ }
+
+ for(int removeId : removeAndSet)
+ if(removeId == i){
+ checkValue = REMOVE_SET_VALUE;
+ break;
+ }
+
+ if(exceptions[i] != null)
+ throw(exceptions[i]);
+ if(x[i] != checkValue)
+ throw(new Throwable("x[" + i + "] =" + x[i]));
+ }
+ }
+}
diff --git a/test/java/lang/ThreadLocal/TestThreadId.java b/test/java/lang/ThreadLocal/TestThreadId.java
new file mode 100644
index 0000000..4380547
--- /dev/null
+++ b/test/java/lang/ThreadLocal/TestThreadId.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6434084
+ * @summary Exercise ThreadLocal javadoc "demo" class ThreadId
+ * @author Pete Soper
+ */
+
+public final class TestThreadId extends Thread {
+
+ // number of times to create threads and gather their ids
+ private static final int ITERATIONCOUNT = 50;
+
+ // Threads constructed per iteration. ITERATIONCOUNT=50 and
+ // THREADCOUNT=50 takes about one second on a sun Blade 1000 (2x750mhz)
+ private static final int THREADCOUNT = 50;
+
+ // The thread local storage object for holding per-thread ids
+ private static ThreadId id = new ThreadId();
+
+ // Holds the per-thread so main method thread can collect it. JMM
+ // guarantees this is valid after this thread joins main method thread.
+ private int value;
+
+ private synchronized int getIdValue() {
+ return value;
+ }
+
+ // Each child thread just publishes its id value for validation
+ public void run() {
+ value = id.get();
+ }
+
+ public static void main(String args[]) throws Throwable {
+
+ // holds true corresponding to a used id value
+ boolean check[] = new boolean[THREADCOUNT*ITERATIONCOUNT];
+
+ // the test threads
+ TestThreadId u[] = new TestThreadId[THREADCOUNT];
+
+ for (int i = 0; i < ITERATIONCOUNT; i++) {
+ // Create and start the threads
+ for (int t=0;t<THREADCOUNT;t++) {
+ u[t] = new TestThreadId();
+ u[t].start();
+ }
+ // Join with each thread and get/check its id
+ for (int t=0;t<THREADCOUNT;t++) {
+ try {
+ u[t].join();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(
+ "TestThreadId: Failed with unexpected exception" + e);
+ }
+ try {
+ if (check[u[t].getIdValue()]) {
+ throw new RuntimeException(
+ "TestThreadId: Failed with duplicated id: " +
+ u[t].getIdValue());
+ } else {
+ check[u[t].getIdValue()] = true;
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "TestThreadId: Failed with unexpected id value" + e);
+ }
+ }
+ }
+ } // main
+} // TestThreadId
diff --git a/test/java/lang/ThreadLocal/ThreadId.java b/test/java/lang/ThreadLocal/ThreadId.java
new file mode 100644
index 0000000..dd58b34
--- /dev/null
+++ b/test/java/lang/ThreadLocal/ThreadId.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ *
+ *
+ * Example class from java.lang.ThreadLocal class doc. Used by
+ * TestThreadId.java
+ */
+
+// Josh Bloch suggested this latest version after many inputs from other
+// EG members and JUC list subscribers
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class ThreadId {
+ // Atomic integer containing the next thread ID to be assigned
+ private static final AtomicInteger nextId = new AtomicInteger(0);
+
+ // Thread local variable containing each thread's ID
+ private static final ThreadLocal<Integer> threadId =
+ new ThreadLocal<Integer>() {
+ @Override protected Integer initialValue() {
+ return nextId.getAndIncrement();
+ }
+ };
+
+ // Returns the current thread's unique ID, assigning it if necessary
+ public static int get() {
+ return threadId.get();
+ }
+}