21 */
22
23 /*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea and Martin Buchholz with assistance from
30 * members of JCP JSR-166 Expert Group and released to the public
31 * domain, as explained at
32 * http://creativecommons.org/publicdomain/zero/1.0/
33 */
34
35 import java.lang.invoke.MethodHandles;
36 import java.lang.invoke.VarHandle;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.ForkJoinPool;
39 import java.util.concurrent.ForkJoinTask;
40 import java.util.concurrent.Future;
41 import java.util.concurrent.ThreadLocalRandom;
42 import java.util.stream.Stream;
43
44 import junit.framework.Test;
45 import junit.framework.TestSuite;
46
47 public class ForkJoinPool9Test extends JSR166TestCase {
48 public static void main(String[] args) {
49 main(suite(), args);
50 }
51
52 public static Test suite() {
53 return new TestSuite(ForkJoinPool9Test.class);
54 }
55
56 /**
57 * Check handling of common pool thread context class loader
58 */
59 public void testCommonPoolThreadContextClassLoader() throws Throwable {
60 if (!testImplementationDetails) return;
61
64 "java.util.concurrent.ForkJoinPool.common.parallelism");
65 if ("0".equals(prop)) return;
66
67 VarHandle CCL =
68 MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
69 .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
70 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
71 boolean haveSecurityManager = (System.getSecurityManager() != null);
72 CountDownLatch runInCommonPoolStarted = new CountDownLatch(1);
73 ClassLoader classLoaderDistinctFromSystemClassLoader
74 = ClassLoader.getPlatformClassLoader();
75 assertNotSame(classLoaderDistinctFromSystemClassLoader,
76 systemClassLoader);
77 Runnable runInCommonPool = () -> {
78 runInCommonPoolStarted.countDown();
79 assertTrue(ForkJoinTask.inForkJoinPool());
80 assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool());
81 Thread currentThread = Thread.currentThread();
82
83 Stream.of(systemClassLoader, null).forEach(cl -> {
84 if (ThreadLocalRandom.current().nextBoolean())
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
93 if (haveSecurityManager)
94 assertThrows(
95 SecurityException.class,
96 () -> System.getProperty("foo"),
97 () -> currentThread.setContextClassLoader(
98 classLoaderDistinctFromSystemClassLoader));
99 // TODO ?
100 // if (haveSecurityManager
101 // && Thread.currentThread().getClass().getSimpleName()
102 // .equals("InnocuousForkJoinWorkerThread"))
103 // assertThrows(SecurityException.class, /* ?? */);
104 };
|
21 */
22
23 /*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea and Martin Buchholz with assistance from
30 * members of JCP JSR-166 Expert Group and released to the public
31 * domain, as explained at
32 * http://creativecommons.org/publicdomain/zero/1.0/
33 */
34
35 import java.lang.invoke.MethodHandles;
36 import java.lang.invoke.VarHandle;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.ForkJoinPool;
39 import java.util.concurrent.ForkJoinTask;
40 import java.util.concurrent.Future;
41 import java.util.stream.Stream;
42
43 import junit.framework.Test;
44 import junit.framework.TestSuite;
45
46 public class ForkJoinPool9Test extends JSR166TestCase {
47 public static void main(String[] args) {
48 main(suite(), args);
49 }
50
51 public static Test suite() {
52 return new TestSuite(ForkJoinPool9Test.class);
53 }
54
55 /**
56 * Check handling of common pool thread context class loader
57 */
58 public void testCommonPoolThreadContextClassLoader() throws Throwable {
59 if (!testImplementationDetails) return;
60
63 "java.util.concurrent.ForkJoinPool.common.parallelism");
64 if ("0".equals(prop)) return;
65
66 VarHandle CCL =
67 MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup())
68 .findVarHandle(Thread.class, "contextClassLoader", ClassLoader.class);
69 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
70 boolean haveSecurityManager = (System.getSecurityManager() != null);
71 CountDownLatch runInCommonPoolStarted = new CountDownLatch(1);
72 ClassLoader classLoaderDistinctFromSystemClassLoader
73 = ClassLoader.getPlatformClassLoader();
74 assertNotSame(classLoaderDistinctFromSystemClassLoader,
75 systemClassLoader);
76 Runnable runInCommonPool = () -> {
77 runInCommonPoolStarted.countDown();
78 assertTrue(ForkJoinTask.inForkJoinPool());
79 assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool());
80 Thread currentThread = Thread.currentThread();
81
82 Stream.of(systemClassLoader, null).forEach(cl -> {
83 if (randomBoolean())
84 // should always be permitted, without effect
85 currentThread.setContextClassLoader(cl);
86 });
87
88 Stream.of(currentThread.getContextClassLoader(),
89 (ClassLoader) CCL.get(currentThread))
90 .forEach(cl -> assertTrue(cl == systemClassLoader || cl == null));
91
92 if (haveSecurityManager)
93 assertThrows(
94 SecurityException.class,
95 () -> System.getProperty("foo"),
96 () -> currentThread.setContextClassLoader(
97 classLoaderDistinctFromSystemClassLoader));
98 // TODO ?
99 // if (haveSecurityManager
100 // && Thread.currentThread().getClass().getSimpleName()
101 // .equals("InnocuousForkJoinWorkerThread"))
102 // assertThrows(SecurityException.class, /* ?? */);
103 };
|