|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +categories: [JDK] |
| 4 | +description: none |
| 5 | +keywords: JDK |
| 6 | +--- |
| 7 | +# JDK源码Runtime |
| 8 | +每一个Java应用程序都一个Runtime类的实例,使得应用程序能够与其运行的环境相连接。 |
| 9 | + |
| 10 | +## Runtime源码 |
| 11 | +``` |
| 12 | +package java.lang; |
| 13 | + |
| 14 | +import sun.reflect.CallerSensitive; |
| 15 | +import sun.reflect.Reflection; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.util.StringTokenizer; |
| 22 | + |
| 23 | +/** |
| 24 | + * Runtime类,里面可以获得应用运行时的一些状态(主要包括使用的内存和cpu个数)和在应用运行时执行一些操作(程序退出、执行gc、设置退出时的钩子函数)。 |
| 25 | + * 用到了单例模式:确保一个类最多只有一个实例,并提供一个全局访问点。 |
| 26 | + */ |
| 27 | +public class Runtime { |
| 28 | + private static Runtime currentRuntime = new Runtime(); |
| 29 | + |
| 30 | + /** |
| 31 | + * 应用了设计模式中的单例模式饿汉式(线程安全) |
| 32 | + * 返回与当前应用程序相关的java运行时对象。 |
| 33 | + */ |
| 34 | + public static Runtime getRuntime() { |
| 35 | + return currentRuntime; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 私有构造函数,单例模式的条件,返回与当前应用程序相关的java运行时对象,不支持new的Runtime |
| 40 | + */ |
| 41 | + private Runtime() { |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 通过启动虚拟机的关闭序列,终止当前正在运行的 Java 虚拟机。此方法从不正常返回。可以将变量作为一个状态码;根据惯例,非零的状态码表示非正常终止。 |
| 46 | + * 虚拟机的关闭序列包含两个阶段。在第一个阶段中,会以某种未指定的顺序启动所有已注册的关闭钩子(hook)(如果有的话),并且允许它们同时运行直至结束。 |
| 47 | + * 在第二个阶段中,如果已启用退出终结,则运行所有未调用的终结方法。一旦完成这个阶段,虚拟机就会暂停。 |
| 48 | + * 如果在虚拟机已开始其关闭序列后才调用此方法,那么若正在运行关闭钩子,则将无限期地阻断此方法。 |
| 49 | + * 如果已经运行完关闭钩子,并且已启用退出终结 (on-exitfinalization),那么此方法将利用给定的状态码(如果状态码是非零值)暂停虚拟机;否则将无限期地阻断虚拟机。 |
| 50 | + */ |
| 51 | + public void exit(int status) { |
| 52 | + SecurityManager security = System.getSecurityManager(); |
| 53 | + if (security != null) { |
| 54 | + security.checkExit(status); |
| 55 | + } |
| 56 | + Shutdown.exit(status); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * 注册新的虚拟机来关闭钩子。 |
| 61 | + */ |
| 62 | + public void addShutdownHook(Thread hook) { |
| 63 | + SecurityManager sm = System.getSecurityManager(); |
| 64 | + if (sm != null) { |
| 65 | + sm.checkPermission(new RuntimePermission("shutdownHooks")); |
| 66 | + } |
| 67 | + ApplicationShutdownHooks.add(hook); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 取消注册某个先前已注册的虚拟机关闭钩子。 |
| 72 | + * 如果指定的钩子先前已注册并且成功地取消注册,则返回 true,其他情况返回 false。 |
| 73 | + */ |
| 74 | + public boolean removeShutdownHook(Thread hook) { |
| 75 | + SecurityManager sm = System.getSecurityManager(); |
| 76 | + if (sm != null) { |
| 77 | + sm.checkPermission(new RuntimePermission("shutdownHooks")); |
| 78 | + } |
| 79 | + return ApplicationShutdownHooks.remove(hook); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 强行终止目前正在运行的 Java 虚拟机。此方法从不正常返回。 |
| 84 | + * 应小心使用此方法。与 exit方法不同,此方法不会启动关闭钩子,并且如果已启用退出终结,此方法也不会运行未调用的终结方法。 |
| 85 | + * 如果已经发起关闭序列,那么此方法不会等待所有正在运行的关闭钩子或终结方法完成其工作。 |
| 86 | + */ |
| 87 | + public void halt(int status) { |
| 88 | + SecurityManager sm = System.getSecurityManager(); |
| 89 | + if (sm != null) { |
| 90 | + sm.checkExit(status); |
| 91 | + } |
| 92 | + Shutdown.halt(status); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * 在退出时启用或禁用终结;这样做可指定拥有未被自动调用终结方法的所有对象的终结方法,并将在退出 Java 运行时前运行此终结方法。默认情况下,禁用退出终结。 |
| 97 | + * 如果有安全管理器,则首先使用 0 作为变量来调用其 checkExit 方法,以确保允许退出。这可能会导致 SecurityException。 |
| 98 | + */ |
| 99 | + @Deprecated |
| 100 | + public static void runFinalizersOnExit(boolean value) { |
| 101 | + SecurityManager security = System.getSecurityManager(); |
| 102 | + if (security != null) { |
| 103 | + try { |
| 104 | + security.checkExit(0); |
| 105 | + } catch (SecurityException e) { |
| 106 | + throw new SecurityException("runFinalizersOnExit"); |
| 107 | + } |
| 108 | + } |
| 109 | + Shutdown.setRunFinalizersOnExit(value); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * 在单独的进程中执行指定的字符串命令。 |
| 114 | + * 对于 exec(command) 形式的调用而言,其行为与调用 exec(command, null, null) 完全相同。 |
| 115 | + */ |
| 116 | + public Process exec(String command) throws IOException { |
| 117 | + return exec(command, null, null); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * 在指定环境的单独进程中执行指定的字符串命令。 |
| 122 | + * 对于 exec(command, envp) 形式的调用而言,其行为与调用 exec(command, envp, null) 完全相同。 |
| 123 | + */ |
| 124 | + public Process exec(String command, String[] envp) throws IOException { |
| 125 | + return exec(command, envp, null); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 在有指定环境和工作目录的独立进程中执行指定的字符串命令。 |
| 130 | + * 对于 exec(command, envp, dir) 形式的调用而言,其行为与调用 exec(cmdarray, envp, dir) 完全相同,其中 cmdarray 是 command 中所有标记的数组。 |
| 131 | + * 更准确地说,可以使用通过调用 new StringTokenizer(command) 创建的 StringTokenizer 将 command 字符串拆解成标记,调用时不对字符类别做进一步的修改。 |
| 132 | + * 然后将标记生成器所生成的标记以相同的顺序放入新的字符串数组 cmdarray 中。 |
| 133 | + */ |
| 134 | + public Process exec(String command, String[] envp, File dir) |
| 135 | + throws IOException { |
| 136 | + if (command.length() == 0) |
| 137 | + throw new IllegalArgumentException("Empty command"); |
| 138 | + |
| 139 | + StringTokenizer st = new StringTokenizer(command); |
| 140 | + String[] cmdarray = new String[st.countTokens()]; |
| 141 | + for (int i = 0; st.hasMoreTokens(); i++) |
| 142 | + cmdarray[i] = st.nextToken(); |
| 143 | + return exec(cmdarray, envp, dir); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * 在单独的进程中执行指定命令和变量。 |
| 148 | + * 对于 exec(cmdarray) 形式的调用而言,其行为与调用 exec(cmdarray, null, null) 完全相同。 |
| 149 | + */ |
| 150 | + public Process exec(String cmdarray[]) throws IOException { |
| 151 | + return exec(cmdarray, null, null); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * 在指定环境的独立进程中执行指定命令和变量。 |
| 156 | + * 对于 exec(cmdarray, envp) 形式的调用而言,其行为与调用 exec(cmdarray, envp, null) 完全相同。 |
| 157 | + */ |
| 158 | + public Process exec(String[] cmdarray, String[] envp) throws IOException { |
| 159 | + return exec(cmdarray, envp, null); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + /** |
| 164 | + * 在指定环境和工作目录的独立进程中执行指定的命令和变量。 |
| 165 | + * 给定的字符串数组 cmdarray 表示一个命令行标记,字符串数组 envp 则表示“环境”变量设置,此方法会创建一个新进程,而指定的命令就在这个进程中执行。 |
| 166 | + * 此方法检查 cmdarray 是否是一条有效的操作系统命令。哪些命令有效取决于系统,但是该命令至少必须有一个非 null 字符串的非空列表。 |
| 167 | + */ |
| 168 | + public Process exec(String[] cmdarray, String[] envp, File dir) |
| 169 | + throws IOException { |
| 170 | + return new ProcessBuilder(cmdarray) |
| 171 | + .environment(envp) |
| 172 | + .directory(dir) |
| 173 | + .start(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * 向 Java 虚拟机返回可用处理器的数目。 |
| 178 | + * 该值在特定的虚拟机调用期间可能发生更改。因此,对可用处理器数目很敏感的应用程序应该不定期地轮询该属性,并相应地调整其资源用法。 |
| 179 | + * 虚拟机可用的最大处理器数目;从不小于 1 |
| 180 | + */ |
| 181 | + public native int availableProcessors(); |
| 182 | + |
| 183 | + /** |
| 184 | + * 返回 Java 虚拟机中的空闲内存量。调用 gc 方法可能导致 freeMemory 返回值的增加。 |
| 185 | + */ |
| 186 | + public native long freeMemory(); |
| 187 | + |
| 188 | + /** |
| 189 | + * 返回 Java 虚拟机中的内存总量。此方法返回的值可能随时间的推移而变化,这取决于主机环境。 |
| 190 | + */ |
| 191 | + public native long totalMemory(); |
| 192 | + |
| 193 | + /** |
| 194 | + * 返回 Java 虚拟机试图使用的最大内存量。如果内存本身没有限制,则返回值 Long.MAX_VALUE。 |
| 195 | + */ |
| 196 | + public native long maxMemory(); |
| 197 | + |
| 198 | + /** |
| 199 | + * 运行垃圾回收器。调用此方法意味着 Java 虚拟机做了一些努力来回收未用对象,以便能够快速地重用这些对象当前占用的内存。 |
| 200 | + * 当控制从方法调用中返回时,虚拟机已经尽最大努力回收了所有丢弃的对象。 |
| 201 | + * 垃圾回收机制主要有两类:引用计数收集器 跟踪收集器 |
| 202 | + */ |
| 203 | + public native void gc(); |
| 204 | + |
| 205 | + /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */ |
| 206 | + private static native void runFinalization0(); |
| 207 | + |
| 208 | + /** |
| 209 | + * 运行挂起 finalization 的所有对象的终止方法。 |
| 210 | + * 调用此方法意味着 Java 虚拟机做了一些努力运行已被丢弃对象的 finalize 方法, |
| 211 | + * 但是这些对象的 finalize 方法还没有运行。当控制从方法调用中返回时,Java 虚拟机已经尽最大努力去完成所有未执行的终止方法。 |
| 212 | + * 如果不显式调用 runFinalization 方法,则 Java 虚拟机会根据需要在单独的线程中自动执行此终止过程。 |
| 213 | + */ |
| 214 | + public void runFinalization() { |
| 215 | + runFinalization0(); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * 启用/禁用指令跟踪。 |
| 220 | + */ |
| 221 | + public native void traceInstructions(boolean on); |
| 222 | + |
| 223 | + /** |
| 224 | + * 启用/禁用方法调用跟踪。 |
| 225 | + */ |
| 226 | + public native void traceMethodCalls(boolean on); |
| 227 | + |
| 228 | + /** |
| 229 | + * 加载具有指定动态库。 |
| 230 | + */ |
| 231 | + @CallerSensitive |
| 232 | + public void load(String filename) { |
| 233 | + load0(Reflection.getCallerClass(), filename); |
| 234 | + } |
| 235 | + |
| 236 | + synchronized void load0(Class<?> fromClass, String filename) { |
| 237 | + SecurityManager security = System.getSecurityManager(); |
| 238 | + if (security != null) { |
| 239 | + security.checkLink(filename); |
| 240 | + } |
| 241 | + if (!(new File(filename).isAbsolute())) { |
| 242 | + throw new UnsatisfiedLinkError( |
| 243 | + "Expecting an absolute path of the library: " + filename); |
| 244 | + } |
| 245 | + ClassLoader.loadLibrary(fromClass, filename, true); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * 加载具有指定动态库。 |
| 250 | + */ |
| 251 | + @CallerSensitive |
| 252 | + public void loadLibrary(String libname) { |
| 253 | + loadLibrary0(Reflection.getCallerClass(), libname); |
| 254 | + } |
| 255 | + |
| 256 | + synchronized void loadLibrary0(Class<?> fromClass, String libname) { |
| 257 | + SecurityManager security = System.getSecurityManager(); |
| 258 | + if (security != null) { |
| 259 | + security.checkLink(libname); |
| 260 | + } |
| 261 | + if (libname.indexOf((int) File.separatorChar) != -1) { |
| 262 | + throw new UnsatisfiedLinkError( |
| 263 | + "Directory separator should not appear in library name: " + libname); |
| 264 | + } |
| 265 | + ClassLoader.loadLibrary(fromClass, libname, false); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * 创建输入流的本地化版本。此方法获取 InputStream,并返回除本地化外其他所有方面都和变量等效的 InputStream,这些方面包括:作为本地字符集中的字符从流中被读取,并将它们从本地字符集自动转换为 Unicode。 |
| 270 | + */ |
| 271 | + @Deprecated |
| 272 | + public InputStream getLocalizedInputStream(InputStream in) { |
| 273 | + return in; |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * 创建输出流的本地化版本。此方法获取 OutputStream,并返回除本地化外其他所有方面都和变量等效的 OutputStream,这些方面包括:作为 Unicode 字符被写入流中,并被自动转换为本地字符集。 |
| 278 | + * 如果参数已经是本地流,则可作为结果返回。 |
| 279 | + */ |
| 280 | + @Deprecated |
| 281 | + public OutputStream getLocalizedOutputStream(OutputStream out) { |
| 282 | + return out; |
| 283 | + } |
| 284 | + |
| 285 | +} |
| 286 | +``` |
| 287 | + |
| 288 | +## gc 方法 |
| 289 | +调用Runtime.getRuntime().gc( )方法来运行JVM的垃圾回收器,System类中的gc( )方法就是如此调用的。 |
| 290 | +``` |
| 291 | +public final class System{ |
| 292 | +... |
| 293 | +public static void gc(){ |
| 294 | + Runtime.getRuntime().gc(); |
| 295 | +} |
| 296 | +... |
| 297 | +} |
| 298 | +``` |
| 299 | + |
| 300 | +在Runtime中gc()是一个native本地方法 |
| 301 | + |
| 302 | +public native void gc( ); |
| 303 | +```java |
| 304 | + |
| 305 | +``` |
| 306 | + |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | + |
| 321 | + |
| 322 | + |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | + |
0 commit comments