java.lang.Runtime类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(188)

本文整理了Java中java.lang.Runtime类的一些代码示例,展示了Runtime类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Runtime类的具体详情如下:
包路径:java.lang.Runtime
类名称:Runtime

Runtime介绍

[英]Allows Java applications to interface with the environment in which they are running. Applications can not create an instance of this class, but they can get a singleton instance by invoking #getRuntime().
[中]允许Java应用程序与运行它们的环境交互。应用程序无法创建此类的实例,但可以通过调用#getRuntime()获得单例实例。

代码示例

代码示例来源:origin: commons-io/commons-io

  1. /**
  2. * Opens the process to the operating system.
  3. *
  4. * @param cmdAttribs the command line parameters
  5. * @return the process
  6. * @throws IOException if an error occurs
  7. */
  8. Process openProcess(final String[] cmdAttribs) throws IOException {
  9. return Runtime.getRuntime().exec(cmdAttribs);
  10. }

代码示例来源:origin: google/guava

  1. @VisibleForTesting
  2. void addShutdownHook(Thread hook) {
  3. Runtime.getRuntime().addShutdownHook(hook);
  4. }
  5. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Create a new {@code Netty4ClientHttpRequestFactory} with a default
  3. * {@link NioEventLoopGroup}.
  4. */
  5. public Netty4ClientHttpRequestFactory() {
  6. int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
  7. this.eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
  8. this.defaultEventLoopGroup = true;
  9. }

代码示例来源:origin: skylot/jadx

  1. public static boolean isFreeMemoryAvailable() {
  2. Runtime runtime = Runtime.getRuntime();
  3. long maxMemory = runtime.maxMemory();
  4. long totalFree = runtime.freeMemory() + (maxMemory - runtime.totalMemory());
  5. return totalFree > MIN_FREE_MEMORY;
  6. }

代码示例来源:origin: libgdx/libgdx

  1. @Override
  2. public long getJavaHeap () {
  3. return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  4. }

代码示例来源:origin: libgdx/libgdx

  1. /** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
  2. * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
  3. public void setHaltOnShutdown (boolean halt) {
  4. if (halt) {
  5. if (shutdownHook != null) return;
  6. shutdownHook = new Thread() {
  7. public void run () {
  8. Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
  9. }
  10. };
  11. Runtime.getRuntime().addShutdownHook(shutdownHook);
  12. } else if (shutdownHook != null) {
  13. Runtime.getRuntime().removeShutdownHook(shutdownHook);
  14. shutdownHook = null;
  15. }
  16. }

代码示例来源:origin: apache/incubator-dubbo

  1. /**
  2. * Unregister the ShutdownHook
  3. */
  4. public void unregister() {
  5. if (registered.get() && registered.compareAndSet(true, false)) {
  6. Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook());
  7. }
  8. }

代码示例来源:origin: square/okhttp

  1. /**
  2. * See FinalizationTester for discussion on how to best trigger GC in tests.
  3. * https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/
  4. * java/lang/ref/FinalizationTester.java
  5. */
  6. public static void awaitGarbageCollection() throws Exception {
  7. Runtime.getRuntime().gc();
  8. Thread.sleep(100);
  9. System.runFinalization();
  10. }
  11. }

代码示例来源:origin: skylot/jadx

  1. public void update() {
  2. long used = runtime.totalMemory() - runtime.freeMemory();
  3. int usedKB = (int) (used / 1024);
  4. setValue(usedKB);
  5. setString(String.format(textFormat, (usedKB / TWO_TO_20), maxGB));
  6. if ((used + Utils.MIN_FREE_MEMORY) > runtime.maxMemory()) {
  7. setForeground(RED);
  8. } else {
  9. setForeground(GREEN);
  10. }
  11. }

代码示例来源:origin: libgdx/libgdx

  1. @Override
  2. public long getJavaHeap () {
  3. return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  4. }

代码示例来源:origin: apache/incubator-dubbo

  1. @Override
  2. public Status check() {
  3. Runtime runtime = Runtime.getRuntime();
  4. long freeMemory = runtime.freeMemory();
  5. long totalMemory = runtime.totalMemory();
  6. long maxMemory = runtime.maxMemory();
  7. boolean ok = (maxMemory - (totalMemory - freeMemory) > 2048); // Alarm when spare memory < 2M
  8. String msg = "max:" + (maxMemory / 1024 / 1024) + "M,total:"
  9. + (totalMemory / 1024 / 1024) + "M,used:" + ((totalMemory / 1024 / 1024) - (freeMemory / 1024 / 1024)) + "M,free:" + (freeMemory / 1024 / 1024) + "M";
  10. return new Status(ok ? Status.Level.OK : Status.Level.WARN, msg);
  11. }

代码示例来源:origin: libgdx/libgdx

  1. /** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
  2. * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
  3. public void setHaltOnShutdown (boolean halt) {
  4. if (halt) {
  5. if (shutdownHook != null) return;
  6. shutdownHook = new Thread() {
  7. public void run () {
  8. Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
  9. }
  10. };
  11. Runtime.getRuntime().addShutdownHook(shutdownHook);
  12. } else if (shutdownHook != null) {
  13. Runtime.getRuntime().removeShutdownHook(shutdownHook);
  14. shutdownHook = null;
  15. }
  16. }

代码示例来源:origin: apache/incubator-dubbo

  1. /**
  2. * Unregister the ShutdownHook
  3. */
  4. public void unregister() {
  5. if (registered.get() && registered.compareAndSet(true, false)) {
  6. Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook());
  7. }
  8. }

代码示例来源:origin: square/leakcanary

  1. @Override public void runGc() {
  2. // Code taken from AOSP FinalizationTest:
  3. // https://android.googlesource.com/platform/libcore/+/master/support/src/test/java/libcore/
  4. // java/lang/ref/FinalizationTester.java
  5. // System.gc() does not garbage collect every time. Runtime.gc() is
  6. // more likely to perform a gc.
  7. Runtime.getRuntime().gc();
  8. enqueueReferences();
  9. System.runFinalization();
  10. }

代码示例来源:origin: gocd/gocd

  1. public void mouseClicked(MouseEvent e) {
  2. // Launch Page
  3. try {
  4. Runtime.getRuntime().exec("open http://localhost:8153/go");
  5. } catch (IOException e1) {
  6. // Don't care
  7. }
  8. }

代码示例来源:origin: hankcs/HanLP

  1. /**
  2. * 开启多线程
  3. * @param enable true表示开启[系统CPU核心数]个线程,false表示单线程
  4. * @return
  5. */
  6. public Segment enableMultithreading(boolean enable)
  7. {
  8. if (enable) config.threadNumber = Runtime.getRuntime().availableProcessors();
  9. else config.threadNumber = 1;
  10. return this;
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Register a shutdown hook with the JVM runtime, closing this context
  3. * on JVM shutdown unless it has already been closed at that time.
  4. * <p>Delegates to {@code doClose()} for the actual closing procedure.
  5. * @see Runtime#addShutdownHook
  6. * @see #close()
  7. * @see #doClose()
  8. */
  9. @Override
  10. public void registerShutdownHook() {
  11. if (this.shutdownHook == null) {
  12. // No shutdown hook registered yet.
  13. this.shutdownHook = new Thread() {
  14. @Override
  15. public void run() {
  16. synchronized (startupShutdownMonitor) {
  17. doClose();
  18. }
  19. }
  20. };
  21. Runtime.getRuntime().addShutdownHook(this.shutdownHook);
  22. }
  23. }

代码示例来源:origin: libgdx/libgdx

  1. @Override
  2. public long getJavaHeap () {
  3. return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  4. }

代码示例来源:origin: apache/incubator-dubbo

  1. @Override
  2. public Status check() {
  3. Runtime runtime = Runtime.getRuntime();
  4. long freeMemory = runtime.freeMemory();
  5. long totalMemory = runtime.totalMemory();
  6. long maxMemory = runtime.maxMemory();
  7. boolean ok = (maxMemory - (totalMemory - freeMemory) > 2048); // Alarm when spare memory < 2M
  8. String msg = "max:" + (maxMemory / 1024 / 1024) + "M,total:"
  9. + (totalMemory / 1024 / 1024) + "M,used:" + ((totalMemory / 1024 / 1024) - (freeMemory / 1024 / 1024)) + "M,free:" + (freeMemory / 1024 / 1024) + "M";
  10. return new Status(ok ? Status.Level.OK : Status.Level.WARN, msg);
  11. }

代码示例来源:origin: libgdx/libgdx

  1. /** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
  2. * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
  3. public void setHaltOnShutdown (boolean halt) {
  4. if (halt) {
  5. if (shutdownHook != null) return;
  6. shutdownHook = new Thread() {
  7. public void run () {
  8. Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
  9. }
  10. };
  11. Runtime.getRuntime().addShutdownHook(shutdownHook);
  12. } else if (shutdownHook != null) {
  13. Runtime.getRuntime().removeShutdownHook(shutdownHook);
  14. shutdownHook = null;
  15. }
  16. }

相关文章