Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/runtime/RuntimeUtil.java
38829 views
/*1* Copyright (c) 2005, 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.rmi.runtime;2627import java.security.AccessController;28import java.security.Permission;29import java.security.PrivilegedAction;30import java.util.concurrent.ScheduledThreadPoolExecutor;31import java.util.concurrent.ThreadFactory;32import java.util.concurrent.TimeUnit;33import java.util.concurrent.atomic.AtomicInteger;34import java.util.logging.Level;35import sun.security.action.GetIntegerAction;3637/**38* RMI runtime implementation utilities.39*40* There is a single instance of this class, which can be obtained41* with a GetInstanceAction. Getting the instance requires42* RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance")43* because the public methods of this class expose security-sensitive44* capabilities.45*46* @author Peter Jones47**/48public final class RuntimeUtil {4950/** runtime package log */51private static final Log runtimeLog =52Log.getLog("sun.rmi.runtime", null, false);5354/** number of scheduler threads */55private static final int schedulerThreads = // default 156AccessController.doPrivileged(57new GetIntegerAction("sun.rmi.runtime.schedulerThreads", 1));5859/** permission required to get instance */60private static final Permission GET_INSTANCE_PERMISSION =61new RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance");6263/** the singleton instance of this class */64private static final RuntimeUtil instance = new RuntimeUtil();6566/** thread pool for scheduling delayed tasks */67private final ScheduledThreadPoolExecutor scheduler;6869private RuntimeUtil() {70scheduler = new ScheduledThreadPoolExecutor(71schedulerThreads,72new ThreadFactory() {73private final AtomicInteger count = new AtomicInteger(0);74public Thread newThread(Runnable runnable) {75try {76return AccessController.doPrivileged(77new NewThreadAction(runnable,78"Scheduler(" + count.getAndIncrement() + ")",79true));80} catch (Throwable t) {81runtimeLog.log(Level.WARNING,82"scheduler thread factory throws", t);83return null;84}85}86});87/*88* We would like to allow the scheduler's threads to terminate89* if possible, but a bug in DelayQueue.poll can cause code90* like this to result in a busy loop:91*/92// stpe.setKeepAliveTime(10, TimeUnit.MINUTES);93// stpe.allowCoreThreadTimeOut(true);94}9596/**97* A PrivilegedAction for getting the RuntimeUtil instance.98**/99public static class GetInstanceAction100implements PrivilegedAction<RuntimeUtil>101{102/**103* Creates an action that returns the RuntimeUtil instance.104**/105public GetInstanceAction() {106}107108public RuntimeUtil run() {109return getInstance();110}111}112113private static RuntimeUtil getInstance() {114SecurityManager sm = System.getSecurityManager();115if (sm != null) {116sm.checkPermission(GET_INSTANCE_PERMISSION);117}118return instance;119}120121/**122* Returns the shared thread pool for scheduling delayed tasks.123*124* Note that the returned pool has limited concurrency, so125* submitted tasks should be short-lived and should not block.126**/127public ScheduledThreadPoolExecutor getScheduler() {128return scheduler;129}130}131132133