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