author | rehn |
Fri, 29 Nov 2019 12:09:25 +0100 | |
changeset 59325 | 3636bab5e81e |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.rmi.runtime; |
|
27 |
||
28 |
import java.security.AccessController; |
|
29 |
import java.security.Permission; |
|
30 |
import java.security.PrivilegedAction; |
|
31 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
32 |
import java.util.concurrent.ThreadFactory; |
|
33 |
import java.util.concurrent.TimeUnit; |
|
34 |
import java.util.concurrent.atomic.AtomicInteger; |
|
35 |
import java.util.logging.Level; |
|
36 |
||
37 |
/** |
|
38 |
* RMI runtime implementation utilities. |
|
39 |
* |
|
40 |
* There is a single instance of this class, which can be obtained |
|
41 |
* with a GetInstanceAction. Getting the instance requires |
|
42 |
* RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance") |
|
43 |
* because the public methods of this class expose security-sensitive |
|
44 |
* capabilities. |
|
45 |
* |
|
46 |
* @author Peter Jones |
|
47 |
**/ |
|
48 |
public final class RuntimeUtil { |
|
49 |
||
50 |
/** runtime package log */ |
|
51 |
private static final Log runtimeLog = |
|
52 |
Log.getLog("sun.rmi.runtime", null, false); |
|
53 |
||
54 |
/** number of scheduler threads */ |
|
55 |
private static final int schedulerThreads = // default 1 |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
5506
diff
changeset
|
56 |
AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
5506
diff
changeset
|
57 |
Integer.getInteger("sun.rmi.runtime.schedulerThreads", 1)); |
2 | 58 |
|
59 |
/** permission required to get instance */ |
|
60 |
private static final Permission GET_INSTANCE_PERMISSION = |
|
61 |
new RuntimePermission("sun.rmi.runtime.RuntimeUtil.getInstance"); |
|
62 |
||
63 |
/** the singleton instance of this class */ |
|
64 |
private static final RuntimeUtil instance = new RuntimeUtil(); |
|
65 |
||
66 |
/** thread pool for scheduling delayed tasks */ |
|
67 |
private final ScheduledThreadPoolExecutor scheduler; |
|
68 |
||
69 |
private RuntimeUtil() { |
|
70 |
scheduler = new ScheduledThreadPoolExecutor( |
|
71 |
schedulerThreads, |
|
72 |
new ThreadFactory() { |
|
73 |
private final AtomicInteger count = new AtomicInteger(0); |
|
74 |
public Thread newThread(Runnable runnable) { |
|
75 |
try { |
|
76 |
return AccessController.doPrivileged( |
|
77 |
new NewThreadAction(runnable, |
|
78 |
"Scheduler(" + count.getAndIncrement() + ")", |
|
79 |
true)); |
|
80 |
} catch (Throwable t) { |
|
81 |
runtimeLog.log(Level.WARNING, |
|
82 |
"scheduler thread factory throws", t); |
|
83 |
return null; |
|
84 |
} |
|
85 |
} |
|
86 |
}); |
|
87 |
/* |
|
88 |
* We would like to allow the scheduler's threads to terminate |
|
89 |
* if possible, but a bug in DelayQueue.poll can cause code |
|
90 |
* like this to result in a busy loop: |
|
91 |
*/ |
|
92 |
// stpe.setKeepAliveTime(10, TimeUnit.MINUTES); |
|
93 |
// stpe.allowCoreThreadTimeOut(true); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* A PrivilegedAction for getting the RuntimeUtil instance. |
|
98 |
**/ |
|
99 |
public static class GetInstanceAction |
|
100 |
implements PrivilegedAction<RuntimeUtil> |
|
101 |
{ |
|
102 |
/** |
|
103 |
* Creates an action that returns the RuntimeUtil instance. |
|
104 |
**/ |
|
105 |
public GetInstanceAction() { |
|
106 |
} |
|
107 |
||
108 |
public RuntimeUtil run() { |
|
109 |
return getInstance(); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
private static RuntimeUtil getInstance() { |
|
114 |
SecurityManager sm = System.getSecurityManager(); |
|
115 |
if (sm != null) { |
|
116 |
sm.checkPermission(GET_INSTANCE_PERMISSION); |
|
117 |
} |
|
118 |
return instance; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Returns the shared thread pool for scheduling delayed tasks. |
|
123 |
* |
|
124 |
* Note that the returned pool has limited concurrency, so |
|
125 |
* submitted tasks should be short-lived and should not block. |
|
126 |
**/ |
|
127 |
public ScheduledThreadPoolExecutor getScheduler() { |
|
128 |
return scheduler; |
|
129 |
} |
|
130 |
} |