1 |
/* |
2 |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
* domain, as explained at |
5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
*/ |
7 |
|
8 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
10 |
import java.lang.invoke.MethodHandles; |
11 |
import java.lang.invoke.VarHandle; |
12 |
import java.util.concurrent.CountDownLatch; |
13 |
import java.util.concurrent.ForkJoinPool; |
14 |
import java.util.concurrent.ForkJoinTask; |
15 |
import java.util.concurrent.Future; |
16 |
|
17 |
import junit.framework.Test; |
18 |
import junit.framework.TestSuite; |
19 |
|
20 |
public class ForkJoinPool9Test extends JSR166TestCase { |
21 |
public static void main(String[] args) { |
22 |
main(suite(), args); |
23 |
} |
24 |
|
25 |
public static Test suite() { |
26 |
return new TestSuite(ForkJoinPool9Test.class); |
27 |
} |
28 |
|
29 |
/** |
30 |
* Check handling of common pool thread context class loader |
31 |
*/ |
32 |
public void testCommonPoolThreadContextClassLoader() throws Throwable { |
33 |
if (!testImplementationDetails) return; |
34 |
|
35 |
// Ensure common pool has at least one real thread |
36 |
String prop = System.getProperty( |
37 |
"java.util.concurrent.ForkJoinPool.common.parallelism"); |
38 |
if ("0".equals(prop)) return; |
39 |
|
40 |
VarHandle CCL = |
41 |
MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()) |
42 |
.findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
43 |
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
44 |
boolean haveSecurityManager = (System.getSecurityManager() != null); |
45 |
CountDownLatch taskStarted = new CountDownLatch(1); |
46 |
Runnable runInCommonPool = () -> { |
47 |
taskStarted.countDown(); |
48 |
assertTrue(ForkJoinTask.inForkJoinPool()); |
49 |
assertSame(ForkJoinPool.commonPool(), |
50 |
ForkJoinTask.getPool()); |
51 |
assertSame(systemClassLoader, |
52 |
Thread.currentThread().getContextClassLoader()); |
53 |
assertSame(systemClassLoader, |
54 |
CCL.get(Thread.currentThread())); |
55 |
if (haveSecurityManager) |
56 |
assertThrows( |
57 |
SecurityException.class, |
58 |
() -> System.getProperty("foo"), |
59 |
() -> Thread.currentThread().setContextClassLoader(null)); |
60 |
// TODO ? |
61 |
// if (haveSecurityManager |
62 |
// && Thread.currentThread().getClass().getSimpleName() |
63 |
// .equals("InnocuousForkJoinWorkerThread")) |
64 |
// assertThrows(SecurityException.class, /* ?? */); |
65 |
}; |
66 |
Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool); |
67 |
// Ensure runInCommonPool is truly running in the common pool, |
68 |
// by giving this thread no opportunity to "help" on get(). |
69 |
await(taskStarted); |
70 |
assertNull(f.get()); |
71 |
} |
72 |
|
73 |
} |