jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
changeset 41131 87edc8451f8a
parent 40817 4f5fb115676d
child 42322 c3474fef4fe4
equal deleted inserted replaced
41130:2004bf22423f 41131:87edc8451f8a
  1189 
  1189 
  1190     /**
  1190     /**
  1191      * Default idle timeout value (in milliseconds) for the thread
  1191      * Default idle timeout value (in milliseconds) for the thread
  1192      * triggering quiescence to park waiting for new work
  1192      * triggering quiescence to park waiting for new work
  1193      */
  1193      */
  1194     private static final long DEFAULT_KEEPALIVE = 60000L;
  1194     private static final long DEFAULT_KEEPALIVE = 60_000L;
  1195 
  1195 
  1196     /**
  1196     /**
  1197      * Undershoot tolerance for idle timeouts
  1197      * Undershoot tolerance for idle timeouts
  1198      */
  1198      */
  1199     private static final long TIMEOUT_SLOP = 20L;
  1199     private static final long TIMEOUT_SLOP = 20L;
  2301             throw new IllegalArgumentException();
  2301             throw new IllegalArgumentException();
  2302         if (factory == null)
  2302         if (factory == null)
  2303             throw new NullPointerException();
  2303             throw new NullPointerException();
  2304         long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
  2304         long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
  2305 
  2305 
  2306         String prefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
       
  2307         int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP);
  2306         int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP);
  2308         long c = ((((long)(-corep)       << TC_SHIFT) & TC_MASK) |
  2307         long c = ((((long)(-corep)       << TC_SHIFT) & TC_MASK) |
  2309                   (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
  2308                   (((long)(-parallelism) << RC_SHIFT) & RC_MASK));
  2310         int m = parallelism | (asyncMode ? FIFO : 0);
  2309         int m = parallelism | (asyncMode ? FIFO : 0);
  2311         int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism;
  2310         int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism;
  2313         int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH);
  2312         int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH);
  2314         int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots
  2313         int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots
  2315         n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
  2314         n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
  2316         n = (n + 1) << 1; // power of two, including space for submission queues
  2315         n = (n + 1) << 1; // power of two, including space for submission queues
  2317 
  2316 
       
  2317         this.workerNamePrefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
  2318         this.workQueues = new WorkQueue[n];
  2318         this.workQueues = new WorkQueue[n];
  2319         this.workerNamePrefix = prefix;
       
  2320         this.factory = factory;
  2319         this.factory = factory;
  2321         this.ueh = handler;
  2320         this.ueh = handler;
  2322         this.saturate = saturate;
  2321         this.saturate = saturate;
  2323         this.keepAlive = ms;
  2322         this.keepAlive = ms;
  2324         this.bounds = b;
  2323         this.bounds = b;
  2325         this.mode = m;
  2324         this.mode = m;
  2326         this.ctl = c;
  2325         this.ctl = c;
  2327         checkPermission();
  2326         checkPermission();
  2328     }
  2327     }
  2329 
  2328 
       
  2329     private Object newInstanceFromSystemProperty(String property)
       
  2330         throws ReflectiveOperationException {
       
  2331         String className = System.getProperty(property);
       
  2332         return (className == null)
       
  2333             ? null
       
  2334             : ClassLoader.getSystemClassLoader().loadClass(className)
       
  2335             .getConstructor().newInstance();
       
  2336     }
       
  2337 
  2330     /**
  2338     /**
  2331      * Constructor for common pool using parameters possibly
  2339      * Constructor for common pool using parameters possibly
  2332      * overridden by system properties
  2340      * overridden by system properties
  2333      */
  2341      */
  2334     @SuppressWarnings("deprecation") // Class.newInstance
       
  2335     private ForkJoinPool(byte forCommonPoolOnly) {
  2342     private ForkJoinPool(byte forCommonPoolOnly) {
  2336         int parallelism = -1;
  2343         int parallelism = -1;
  2337         ForkJoinWorkerThreadFactory fac = null;
  2344         ForkJoinWorkerThreadFactory fac = null;
  2338         UncaughtExceptionHandler handler = null;
  2345         UncaughtExceptionHandler handler = null;
  2339         try {  // ignore exceptions in accessing/parsing properties
  2346         try {  // ignore exceptions in accessing/parsing properties
  2340             String pp = System.getProperty
  2347             String pp = System.getProperty
  2341                 ("java.util.concurrent.ForkJoinPool.common.parallelism");
  2348                 ("java.util.concurrent.ForkJoinPool.common.parallelism");
  2342             String fp = System.getProperty
       
  2343                 ("java.util.concurrent.ForkJoinPool.common.threadFactory");
       
  2344             String hp = System.getProperty
       
  2345                 ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
       
  2346             if (pp != null)
  2349             if (pp != null)
  2347                 parallelism = Integer.parseInt(pp);
  2350                 parallelism = Integer.parseInt(pp);
  2348             if (fp != null)
  2351             fac = (ForkJoinWorkerThreadFactory) newInstanceFromSystemProperty(
  2349                 fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
  2352                 "java.util.concurrent.ForkJoinPool.common.threadFactory");
  2350                            getSystemClassLoader().loadClass(fp).newInstance());
  2353             handler = (UncaughtExceptionHandler) newInstanceFromSystemProperty(
  2351             if (hp != null)
  2354                 "java.util.concurrent.ForkJoinPool.common.exceptionHandler");
  2352                 handler = ((UncaughtExceptionHandler)ClassLoader.
       
  2353                            getSystemClassLoader().loadClass(hp).newInstance());
       
  2354         } catch (Exception ignore) {
  2355         } catch (Exception ignore) {
  2355         }
  2356         }
  2356 
  2357 
  2357         if (fac == null) {
  2358         if (fac == null) {
  2358             if (System.getSecurityManager() == null)
  2359             if (System.getSecurityManager() == null)
  2371         int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH);
  2372         int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH);
  2372         int n = (parallelism > 1) ? parallelism - 1 : 1;
  2373         int n = (parallelism > 1) ? parallelism - 1 : 1;
  2373         n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
  2374         n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
  2374         n = (n + 1) << 1;
  2375         n = (n + 1) << 1;
  2375 
  2376 
       
  2377         this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
  2376         this.workQueues = new WorkQueue[n];
  2378         this.workQueues = new WorkQueue[n];
  2377         this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
       
  2378         this.factory = fac;
  2379         this.factory = fac;
  2379         this.ueh = handler;
  2380         this.ueh = handler;
  2380         this.saturate = null;
  2381         this.saturate = null;
  2381         this.keepAlive = DEFAULT_KEEPALIVE;
  2382         this.keepAlive = DEFAULT_KEEPALIVE;
  2382         this.bounds = b;
  2383         this.bounds = b;