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; |
41 |
|
.findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
42 |
|
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
43 |
|
boolean haveSecurityManager = (System.getSecurityManager() != null); |
44 |
< |
CountDownLatch taskStarted = new CountDownLatch(1); |
44 |
> |
CountDownLatch runInCommonPoolStarted = new CountDownLatch(1); |
45 |
|
ClassLoader classLoaderDistinctFromSystemClassLoader |
46 |
|
= ClassLoader.getPlatformClassLoader(); |
47 |
|
assertNotSame(classLoaderDistinctFromSystemClassLoader, |
48 |
|
systemClassLoader); |
49 |
|
Runnable runInCommonPool = () -> { |
50 |
< |
taskStarted.countDown(); |
50 |
> |
runInCommonPoolStarted.countDown(); |
51 |
|
assertTrue(ForkJoinTask.inForkJoinPool()); |
52 |
< |
assertSame(ForkJoinPool.commonPool(), |
53 |
< |
ForkJoinTask.getPool()); |
54 |
< |
assertSame(systemClassLoader, |
55 |
< |
Thread.currentThread().getContextClassLoader()); |
56 |
< |
assertSame(systemClassLoader, |
57 |
< |
CCL.get(Thread.currentThread())); |
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 |
< |
() -> Thread.currentThread().setContextClassLoader( |
69 |
> |
() -> currentThread.setContextClassLoader( |
70 |
|
classLoaderDistinctFromSystemClassLoader)); |
71 |
|
// TODO ? |
72 |
|
// if (haveSecurityManager |
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(taskStarted); |
80 |
> |
await(runInCommonPoolStarted); |
81 |
|
assertNull(f.get()); |
82 |
|
} |
83 |
|
|