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

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

本文整理了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

/**
 * Opens the process to the operating system.
 *
 * @param cmdAttribs  the command line parameters
 * @return the process
 * @throws IOException if an error occurs
 */
Process openProcess(final String[] cmdAttribs) throws IOException {
  return Runtime.getRuntime().exec(cmdAttribs);
}

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

@VisibleForTesting
 void addShutdownHook(Thread hook) {
  Runtime.getRuntime().addShutdownHook(hook);
 }
}

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

/**
 * Create a new {@code Netty4ClientHttpRequestFactory} with a default
 * {@link NioEventLoopGroup}.
 */
public Netty4ClientHttpRequestFactory() {
  int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
  this.eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
  this.defaultEventLoopGroup = true;
}

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

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

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

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

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

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

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

/**
 * Unregister the ShutdownHook
 */
public void unregister() {
  if (registered.get() && registered.compareAndSet(true, false)) {
    Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook());
  }
}

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

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

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

public void update() {
  long used = runtime.totalMemory() - runtime.freeMemory();
  int usedKB = (int) (used / 1024);
  setValue(usedKB);
  setString(String.format(textFormat, (usedKB / TWO_TO_20), maxGB));
  if ((used + Utils.MIN_FREE_MEMORY) > runtime.maxMemory()) {
    setForeground(RED);
  } else {
    setForeground(GREEN);
  }
}

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

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

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

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

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

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

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

/**
 * Unregister the ShutdownHook
 */
public void unregister() {
  if (registered.get() && registered.compareAndSet(true, false)) {
    Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook());
  }
}

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

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

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

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

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

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

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

/**
 * Register a shutdown hook with the JVM runtime, closing this context
 * on JVM shutdown unless it has already been closed at that time.
 * <p>Delegates to {@code doClose()} for the actual closing procedure.
 * @see Runtime#addShutdownHook
 * @see #close()
 * @see #doClose()
 */
@Override
public void registerShutdownHook() {
  if (this.shutdownHook == null) {
    // No shutdown hook registered yet.
    this.shutdownHook = new Thread() {
      @Override
      public void run() {
        synchronized (startupShutdownMonitor) {
          doClose();
        }
      }
    };
    Runtime.getRuntime().addShutdownHook(this.shutdownHook);
  }
}

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

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

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

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

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

/** When true, <code>Runtime.getRuntime().halt(0);</code> is used when the JVM shuts down. This prevents Swing shutdown hooks
 * from causing a deadlock and keeping the JVM alive indefinitely. Default is true. */
public void setHaltOnShutdown (boolean halt) {
  if (halt) {
    if (shutdownHook != null) return;
    shutdownHook = new Thread() {
      public void run () {
        Runtime.getRuntime().halt(0); // Because fuck you, deadlock causing Swing shutdown hooks.
      }
    };
    Runtime.getRuntime().addShutdownHook(shutdownHook);
  } else if (shutdownHook != null) {
    Runtime.getRuntime().removeShutdownHook(shutdownHook);
    shutdownHook = null;
  }
}

相关文章