36 import java.lang.invoke.VarHandle; |
36 import java.lang.invoke.VarHandle; |
37 import java.util.concurrent.CountDownLatch; |
37 import java.util.concurrent.CountDownLatch; |
38 import java.util.concurrent.ForkJoinPool; |
38 import java.util.concurrent.ForkJoinPool; |
39 import java.util.concurrent.ForkJoinTask; |
39 import java.util.concurrent.ForkJoinTask; |
40 import java.util.concurrent.Future; |
40 import java.util.concurrent.Future; |
|
41 import java.util.concurrent.ThreadLocalRandom; |
|
42 import java.util.stream.Stream; |
41 |
43 |
42 import junit.framework.Test; |
44 import junit.framework.Test; |
43 import junit.framework.TestSuite; |
45 import junit.framework.TestSuite; |
44 |
46 |
45 public class ForkJoinPool9Test extends JSR166TestCase { |
47 public class ForkJoinPool9Test extends JSR166TestCase { |
65 VarHandle CCL = |
67 VarHandle CCL = |
66 MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()) |
68 MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup()) |
67 .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
69 .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class); |
68 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
70 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); |
69 boolean haveSecurityManager = (System.getSecurityManager() != null); |
71 boolean haveSecurityManager = (System.getSecurityManager() != null); |
70 CountDownLatch taskStarted = new CountDownLatch(1); |
72 CountDownLatch runInCommonPoolStarted = new CountDownLatch(1); |
|
73 ClassLoader classLoaderDistinctFromSystemClassLoader |
|
74 = ClassLoader.getPlatformClassLoader(); |
|
75 assertNotSame(classLoaderDistinctFromSystemClassLoader, |
|
76 systemClassLoader); |
71 Runnable runInCommonPool = () -> { |
77 Runnable runInCommonPool = () -> { |
72 taskStarted.countDown(); |
78 runInCommonPoolStarted.countDown(); |
73 assertTrue(ForkJoinTask.inForkJoinPool()); |
79 assertTrue(ForkJoinTask.inForkJoinPool()); |
74 assertSame(ForkJoinPool.commonPool(), |
80 assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool()); |
75 ForkJoinTask.getPool()); |
81 Thread currentThread = Thread.currentThread(); |
76 assertSame(systemClassLoader, |
82 |
77 Thread.currentThread().getContextClassLoader()); |
83 Stream.of(systemClassLoader, null).forEach(cl -> { |
78 assertSame(systemClassLoader, |
84 if (ThreadLocalRandom.current().nextBoolean()) |
79 CCL.get(Thread.currentThread())); |
85 // should always be permitted, without effect |
|
86 currentThread.setContextClassLoader(cl); |
|
87 }); |
|
88 |
|
89 Stream.of(currentThread.getContextClassLoader(), |
|
90 (ClassLoader) CCL.get(currentThread)) |
|
91 .forEach(cl -> assertTrue(cl == systemClassLoader || cl == null)); |
|
92 |
80 if (haveSecurityManager) |
93 if (haveSecurityManager) |
81 assertThrows( |
94 assertThrows( |
82 SecurityException.class, |
95 SecurityException.class, |
83 () -> System.getProperty("foo"), |
96 () -> System.getProperty("foo"), |
84 () -> Thread.currentThread().setContextClassLoader(null)); |
97 () -> currentThread.setContextClassLoader( |
|
98 classLoaderDistinctFromSystemClassLoader)); |
85 // TODO ? |
99 // TODO ? |
86 // if (haveSecurityManager |
100 // if (haveSecurityManager |
87 // && Thread.currentThread().getClass().getSimpleName() |
101 // && Thread.currentThread().getClass().getSimpleName() |
88 // .equals("InnocuousForkJoinWorkerThread")) |
102 // .equals("InnocuousForkJoinWorkerThread")) |
89 // assertThrows(SecurityException.class, /* ?? */); |
103 // assertThrows(SecurityException.class, /* ?? */); |
90 }; |
104 }; |
91 Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool); |
105 Future<?> f = ForkJoinPool.commonPool().submit(runInCommonPool); |
92 // Ensure runInCommonPool is truly running in the common pool, |
106 // Ensure runInCommonPool is truly running in the common pool, |
93 // by giving this thread no opportunity to "help" on get(). |
107 // by giving this thread no opportunity to "help" on get(). |
94 await(taskStarted); |
108 await(runInCommonPoolStarted); |
95 assertNull(f.get()); |
109 assertNull(f.get()); |
96 } |
110 } |
97 |
111 |
98 } |
112 } |