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.CompletableFuture; |
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; |
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 |
< |
CompletableFuture.runAsync( |
46 |
< |
() -> { |
47 |
< |
assertSame(systemClassLoader, |
48 |
< |
Thread.currentThread().getContextClassLoader()); |
49 |
< |
assertSame(systemClassLoader, |
50 |
< |
CCL.get(Thread.currentThread())); |
51 |
< |
if (haveSecurityManager) |
52 |
< |
assertThrows( |
53 |
< |
SecurityException.class, |
54 |
< |
() -> System.getProperty("foo"), |
55 |
< |
() -> Thread.currentThread().setContextClassLoader(null)); |
56 |
< |
|
57 |
< |
// TODO ? |
58 |
< |
// if (haveSecurityManager |
59 |
< |
// && Thread.currentThread().getClass().getSimpleName() |
60 |
< |
// .equals("InnocuousForkJoinWorkerThread")) |
61 |
< |
// assertThrows(SecurityException.class, /* ?? */); |
62 |
< |
}).join(); |
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 |
> |
assertTrue(taskStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
70 |
> |
assertNull(f.get()); |
71 |
|
} |
72 |
|
|
73 |
|
} |