Path: blob/master/src/java.rmi/share/classes/sun/rmi/runtime/RuntimeUtil.java
41153 views
/*1* Copyright (c) 2005, 2021, 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.atomic.AtomicInteger;33import java.util.logging.Level;3435/**36* RMI runtime implementation utilities.37*38* There is a single instance of this class, which can be obtained39* with a GetInstanceAction. Getting the instance requires40* RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance")41* because the public methods of this class expose security-sensitive42* capabilities.43*44* @author Peter Jones45**/46public final class RuntimeUtil {4748/** runtime package log */49private static final Log runtimeLog =50Log.getLog("sun.rmi.runtime", null, false);5152/** number of scheduler threads */53@SuppressWarnings("removal")54private static final int schedulerThreads = // default 155AccessController.doPrivileged((PrivilegedAction<Integer>) () ->56Integer.getInteger("sun.rmi.runtime.schedulerThreads", 1));5758/** permission required to get instance */59private static final Permission GET_INSTANCE_PERMISSION =60new RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance");6162/** the singleton instance of this class */63private static final RuntimeUtil instance = new RuntimeUtil();6465/** thread pool for scheduling delayed tasks */66private final ScheduledThreadPoolExecutor scheduler;6768private RuntimeUtil() {69scheduler = new ScheduledThreadPoolExecutor(70schedulerThreads,71new ThreadFactory() {72private final AtomicInteger count = new AtomicInteger();73@SuppressWarnings("removal")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() {114@SuppressWarnings("removal")115SecurityManager sm = System.getSecurityManager();116if (sm != null) {117sm.checkPermission(GET_INSTANCE_PERMISSION);118}119return instance;120}121122/**123* Returns the shared thread pool for scheduling delayed tasks.124*125* Note that the returned pool has limited concurrency, so126* submitted tasks should be short-lived and should not block.127**/128public ScheduledThreadPoolExecutor getScheduler() {129return scheduler;130}131}132133134