7 |
|
|
8 |
|
import java.lang.invoke.MethodHandles; |
9 |
|
import java.lang.invoke.VarHandle; |
10 |
< |
import java.util.concurrent.CompletableFuture; |
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 |
|
|
15 |
|
import junit.framework.Test; |
16 |
|
import junit.framework.TestSuite; |
29 |
|
*/ |
30 |
|
public void testCommonPoolThreadContextClassLoader() throws Throwable { |
31 |
|
if (!testImplementationDetails) return; |
32 |
+ |
|
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 |
|
VarHandle CCL = |
39 |
|
MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()) |
40 |
|
.findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
41 |
|
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
42 |
|
boolean haveSecurityManager = (System.getSecurityManager() != null); |
43 |
< |
CompletableFuture.runAsync( |
44 |
< |
() -> { |
45 |
< |
assertSame(systemClassLoader, |
46 |
< |
Thread.currentThread().getContextClassLoader()); |
47 |
< |
assertSame(systemClassLoader, |
48 |
< |
CCL.get(Thread.currentThread())); |
49 |
< |
if (haveSecurityManager) |
50 |
< |
assertThrows( |
51 |
< |
SecurityException.class, |
52 |
< |
() -> System.getProperty("foo"), |
53 |
< |
() -> Thread.currentThread().setContextClassLoader(null)); |
54 |
< |
|
55 |
< |
// TODO ? |
56 |
< |
// if (haveSecurityManager |
57 |
< |
// && Thread.currentThread().getClass().getSimpleName() |
58 |
< |
// .equals("InnocuousForkJoinWorkerThread")) |
59 |
< |
// assertThrows(SecurityException.class, /* ?? */); |
60 |
< |
}).join(); |
43 |
> |
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 |
> |
await(taskStarted); |
68 |
> |
assertNull(f.get()); |
69 |
|
} |
70 |
|
|
71 |
|
} |