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 java.lang.invoke.MethodHandles; |
9 |
import java.lang.invoke.VarHandle; |
10 |
import java.util.concurrent.CountDownLatch; |
11 |
import java.util.concurrent.ForkJoinPool; |
12 |
import java.util.concurrent.ForkJoinTask; |
13 |
import java.util.concurrent.Future; |
14 |
import java.util.stream.Stream; |
15 |
|
16 |
import junit.framework.Test; |
17 |
import junit.framework.TestSuite; |
18 |
|
19 |
public class ForkJoinPool9Test extends JSR166TestCase { |
20 |
public static void main(String[] args) { |
21 |
main(suite(), args); |
22 |
} |
23 |
|
24 |
public static Test suite() { |
25 |
return new TestSuite(ForkJoinPool9Test.class); |
26 |
} |
27 |
|
28 |
/** |
29 |
* Check handling of common pool thread context class loader |
30 |
*/ |
31 |
public void testCommonPoolThreadContextClassLoader() throws Throwable { |
32 |
if (!testImplementationDetails) return; |
33 |
|
34 |
// Ensure common pool has at least one real thread |
35 |
String prop = System.getProperty( |
36 |
"java.util.concurrent.ForkJoinPool.common.parallelism"); |
37 |
if ("0".equals(prop)) return; |
38 |
|
39 |
VarHandle CCL = |
40 |
MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()) |
41 |
.findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
42 |
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
43 |
boolean haveSecurityManager = (System.getSecurityManager() != null); |
44 |
CountDownLatch runInCommonPoolStarted = new CountDownLatch(1); |
45 |
ClassLoader classLoaderDistinctFromSystemClassLoader |
46 |
= ClassLoader.getPlatformClassLoader(); |
47 |
assertNotSame(classLoaderDistinctFromSystemClassLoader, |
48 |
systemClassLoader); |
49 |
Runnable runInCommonPool = () -> { |
50 |
runInCommonPoolStarted.countDown(); |
51 |
assertTrue(ForkJoinTask.inForkJoinPool()); |
52 |
assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool()); |
53 |
Thread currentThread = Thread.currentThread(); |
54 |
|
55 |
Stream.of(systemClassLoader, null).forEach(cl -> { |
56 |
if (randomBoolean()) |
57 |
// should always be permitted, without effect |
58 |
currentThread.setContextClassLoader(cl); |
59 |
}); |
60 |
|
61 |
Stream.of(currentThread.getContextClassLoader(), |
62 |
(ClassLoader) CCL.get(currentThread)) |
63 |
.forEach(cl -> assertTrue(cl == systemClassLoader || cl == null)); |
64 |
|
65 |
if (haveSecurityManager) |
66 |
assertThrows( |
67 |
SecurityException.class, |
68 |
() -> System.getProperty("foo"), |
69 |
() -> currentThread.setContextClassLoader( |
70 |
classLoaderDistinctFromSystemClassLoader)); |
71 |
// TODO ? |
72 |
// if (haveSecurityManager |
73 |
// && Thread.currentThread().getClass().getSimpleName() |
74 |
// .equals("InnocuousForkJoinWorkerThread")) |
75 |
// assertThrows(SecurityException.class, /* ?? */); |
76 |
}; |
77 |
Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool); |
78 |
// Ensure runInCommonPool is truly running in the common pool, |
79 |
// by giving this thread no opportunity to "help" on get(). |
80 |
await(runInCommonPoolStarted); |
81 |
assertNull(f.get()); |
82 |
} |
83 |
|
84 |
} |