undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
66
test/jdk/java/lang/ThreadLocal/Basic.java
Normal file
66
test/jdk/java/lang/ThreadLocal/Basic.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 1999, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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]));
|
||||
}
|
||||
}
|
||||
53
test/jdk/java/lang/ThreadLocal/ImmutableLocal.java
Normal file
53
test/jdk/java/lang/ThreadLocal/ImmutableLocal.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2006, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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.
|
||||
*/
|
||||
public abstract static class ImmutableThreadLocal extends ThreadLocal {
|
||||
public void set(final Object value) {
|
||||
throw new RuntimeException("ImmutableThreadLocal set called");
|
||||
}
|
||||
|
||||
// force override
|
||||
protected abstract 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());
|
||||
}
|
||||
}
|
||||
74
test/jdk/java/lang/ThreadLocal/InitialValue.java
Normal file
74
test/jdk/java/lang/ThreadLocal/InitialValue.java
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
39
test/jdk/java/lang/ThreadLocal/MemoryLeak.java
Normal file
39
test/jdk/java/lang/ThreadLocal/MemoryLeak.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2001, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
83
test/jdk/java/lang/ThreadLocal/ReplaceStaleEntry.java
Normal file
83
test/jdk/java/lang/ThreadLocal/ReplaceStaleEntry.java
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8209824
|
||||
* @summary per latest JDK code coverage report, 2 methods replaceStaleEntry
|
||||
* and prevIndex in ThreadLocal.ThreadLocalMap are not touched
|
||||
* by any JDK regression tests, this is to trigger the code paths.
|
||||
* @modules java.base/java.lang:+open
|
||||
* @run testng ReplaceStaleEntry
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class ReplaceStaleEntry {
|
||||
|
||||
public static int INITIAL_CAPACITY;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws Exception {
|
||||
Class<?> clazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
|
||||
Field f = clazz.getDeclaredField("INITIAL_CAPACITY");
|
||||
f.setAccessible(true);
|
||||
INITIAL_CAPACITY = f.getInt(null);
|
||||
System.out.println("INITIAL_CAPACITY: " + INITIAL_CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test triggers the code path to replaceStaleEntry, so as prevIndex is
|
||||
* triggered too as it's called by replaceStaleEntry.
|
||||
* The code paths must be triggered by reusing an already used entry in the
|
||||
* map's entry table. The code below
|
||||
* 1. first occupies the entries
|
||||
* 2. then expunges the entries by removing strong references to thread locals
|
||||
* 3. then reuse some of entries by adding more thread locals to the thread
|
||||
* and, the above steps are run for several times by adding more and more
|
||||
* thread locals, also trigger rehash of the map.
|
||||
*/
|
||||
@Test
|
||||
public static void test() {
|
||||
Map<Integer, ThreadLocal> locals = new HashMap<>();
|
||||
for (int j = 1; j < 32; j *= 2) {
|
||||
int loop = INITIAL_CAPACITY * j;
|
||||
for (int i = 0; i < loop; i++) {
|
||||
ThreadLocal l = new ThreadLocal<Integer>();
|
||||
l.set(i);
|
||||
locals.put(i, l);
|
||||
}
|
||||
locals.keySet().stream().forEach(i -> {
|
||||
assertEquals((int)locals.get(i).get(), (int)i, "Unexpected thread local value!");
|
||||
});
|
||||
locals.clear();
|
||||
System.gc();
|
||||
}
|
||||
}
|
||||
}
|
||||
115
test/jdk/java/lang/ThreadLocal/TLRemoveTest.java
Normal file
115
test/jdk/java/lang/ThreadLocal/TLRemoveTest.java
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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]));
|
||||
}
|
||||
}
|
||||
}
|
||||
93
test/jdk/java/lang/ThreadLocal/TestThreadId.java
Normal file
93
test/jdk/java/lang/ThreadLocal/TestThreadId.java
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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
|
||||
52
test/jdk/java/lang/ThreadLocal/ThreadId.java
Normal file
52
test/jdk/java/lang/ThreadLocal/ThreadId.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.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();
|
||||
}
|
||||
}
|
||||
125
test/jdk/java/lang/ThreadLocal/ThreadLocalSupplierTest.java
Normal file
125
test/jdk/java/lang/ThreadLocal/ThreadLocalSupplierTest.java
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Supplier;
|
||||
import org.testng.annotations.Test;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @run testng ThreadLocalSupplierTest
|
||||
* @summary tests ThreadLocal.withInitial(<Supplier>).
|
||||
* Adapted from java.lang.Basic functional test of ThreadLocal
|
||||
*
|
||||
* @author Jim Gish <jim.gish@oracle.com>
|
||||
*/
|
||||
@Test
|
||||
public class ThreadLocalSupplierTest {
|
||||
|
||||
static final class IntegerSupplier implements Supplier<Integer> {
|
||||
|
||||
private final AtomicInteger supply = new AtomicInteger(0);
|
||||
|
||||
@Override
|
||||
public Integer get() {
|
||||
return supply.getAndIncrement();
|
||||
}
|
||||
|
||||
public int numCalls() {
|
||||
return supply.intValue();
|
||||
}
|
||||
}
|
||||
|
||||
static IntegerSupplier theSupply = new IntegerSupplier();
|
||||
|
||||
static final class MyThreadLocal extends ThreadLocal<Integer> {
|
||||
|
||||
private final ThreadLocal<Integer> delegate;
|
||||
|
||||
public volatile boolean everCalled;
|
||||
|
||||
public MyThreadLocal(Supplier<Integer> supplier) {
|
||||
delegate = ThreadLocal.<Integer>withInitial(supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer get() {
|
||||
return delegate.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized Integer initialValue() {
|
||||
// this should never be called since we are using the factory instead
|
||||
everCalled = true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Our one and only ThreadLocal from which we get thread ids using a
|
||||
* supplier which simply increments a counter on each call of get().
|
||||
*/
|
||||
static MyThreadLocal threadLocal = new MyThreadLocal(theSupply);
|
||||
|
||||
public void testMultiThread() throws Exception {
|
||||
final int threadCount = 500;
|
||||
final Thread th[] = new Thread[threadCount];
|
||||
final boolean visited[] = new boolean[threadCount];
|
||||
|
||||
// Create and start the threads
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
th[i] = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
final int threadId = threadLocal.get();
|
||||
assertFalse(visited[threadId], "visited[" + threadId + "]=" + visited[threadId]);
|
||||
visited[threadId] = true;
|
||||
// check the get() again
|
||||
final int secondCheckThreadId = threadLocal.get();
|
||||
assertEquals(secondCheckThreadId, threadId);
|
||||
}
|
||||
};
|
||||
th[i].start();
|
||||
}
|
||||
|
||||
// Wait for the threads to finish
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
th[i].join();
|
||||
}
|
||||
|
||||
assertEquals(theSupply.numCalls(), threadCount);
|
||||
// make sure the provided initialValue() has not been called
|
||||
assertFalse(threadLocal.everCalled);
|
||||
// Check results
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
assertTrue(visited[i], "visited[" + i + "]=" + visited[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimple() {
|
||||
final String expected = "OneWithEverything";
|
||||
final ThreadLocal<String> threadLocal = ThreadLocal.<String>withInitial(() -> expected);
|
||||
assertEquals(expected, threadLocal.get());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue