Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/ThreadPool.java
38918 views
/*1* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.util.concurrent.*;28import java.security.AccessController;29import java.security.PrivilegedAction;30import sun.security.action.GetPropertyAction;31import sun.security.action.GetIntegerAction;3233/**34* Encapsulates a thread pool associated with a channel group.35*/3637public class ThreadPool {38private static final String DEFAULT_THREAD_POOL_THREAD_FACTORY =39"java.nio.channels.DefaultThreadPool.threadFactory";40private static final String DEFAULT_THREAD_POOL_INITIAL_SIZE =41"java.nio.channels.DefaultThreadPool.initialSize";4243private final ExecutorService executor;4445// indicates if thread pool is fixed size46private final boolean isFixed;4748// indicates the pool size (for a fixed thread pool configuratin this is49// the maximum pool size; for other thread pools it is the initial size)50private final int poolSize;5152private ThreadPool(ExecutorService executor,53boolean isFixed,54int poolSize)55{56this.executor = executor;57this.isFixed = isFixed;58this.poolSize = poolSize;59}6061ExecutorService executor() {62return executor;63}6465boolean isFixedThreadPool() {66return isFixed;67}6869int poolSize() {70return poolSize;71}7273static ThreadFactory defaultThreadFactory() {74if (System.getSecurityManager() == null) {75return (Runnable r) -> {76Thread t = new Thread(r);77t.setDaemon(true);78return t;79};80} else {81return (Runnable r) -> {82PrivilegedAction<Thread> action = () -> {83Thread t = new sun.misc.InnocuousThread(r);84t.setDaemon(true);85return t;86};87return AccessController.doPrivileged(action);88};89}90}9192private static class DefaultThreadPoolHolder {93final static ThreadPool defaultThreadPool = createDefault();94}9596// return the default (system-wide) thread pool97static ThreadPool getDefault() {98return DefaultThreadPoolHolder.defaultThreadPool;99}100101// create thread using default settings (configured by system properties)102static ThreadPool createDefault() {103// default the number of fixed threads to the hardware core count104int initialSize = getDefaultThreadPoolInitialSize();105if (initialSize < 0)106initialSize = Runtime.getRuntime().availableProcessors();107// default to thread factory that creates daemon threads108ThreadFactory threadFactory = getDefaultThreadPoolThreadFactory();109if (threadFactory == null)110threadFactory = defaultThreadFactory();111// create thread pool112ExecutorService executor = Executors.newCachedThreadPool(threadFactory);113return new ThreadPool(executor, false, initialSize);114}115116// create using given parameters117static ThreadPool create(int nThreads, ThreadFactory factory) {118if (nThreads <= 0)119throw new IllegalArgumentException("'nThreads' must be > 0");120ExecutorService executor = Executors.newFixedThreadPool(nThreads, factory);121return new ThreadPool(executor, true, nThreads);122}123124// wrap a user-supplied executor125public static ThreadPool wrap(ExecutorService executor, int initialSize) {126if (executor == null)127throw new NullPointerException("'executor' is null");128// attempt to check if cached thread pool129if (executor instanceof ThreadPoolExecutor) {130int max = ((ThreadPoolExecutor)executor).getMaximumPoolSize();131if (max == Integer.MAX_VALUE) {132if (initialSize < 0) {133initialSize = Runtime.getRuntime().availableProcessors();134} else {135// not a cached thread pool so ignore initial size136initialSize = 0;137}138}139} else {140// some other type of thread pool141if (initialSize < 0)142initialSize = 0;143}144return new ThreadPool(executor, false, initialSize);145}146147private static int getDefaultThreadPoolInitialSize() {148String propValue = AccessController.doPrivileged(new149GetPropertyAction(DEFAULT_THREAD_POOL_INITIAL_SIZE));150if (propValue != null) {151try {152return Integer.parseInt(propValue);153} catch (NumberFormatException x) {154throw new Error("Value of property '" + DEFAULT_THREAD_POOL_INITIAL_SIZE +155"' is invalid: " + x);156}157}158return -1;159}160161private static ThreadFactory getDefaultThreadPoolThreadFactory() {162String propValue = AccessController.doPrivileged(new163GetPropertyAction(DEFAULT_THREAD_POOL_THREAD_FACTORY));164if (propValue != null) {165try {166Class<?> c = Class167.forName(propValue, true, ClassLoader.getSystemClassLoader());168return ((ThreadFactory)c.newInstance());169} catch (ClassNotFoundException x) {170throw new Error(x);171} catch (InstantiationException x) {172throw new Error(x);173} catch (IllegalAccessException x) {174throw new Error(x);175}176}177return null;178}179}180181182