java.lang.Runtime.getRuntime()方法的使用及代码示例

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

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

Runtime.getRuntime介绍

[英]Returns the single Runtime instance for the current application.
[中]返回当前应用程序的单个运行时实例。

代码示例

代码示例来源: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: 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: apache/incubator-dubbo

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

代码示例来源: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: apache/incubator-dubbo

/**
 * Register the ShutdownHook
 */
public void register() {
  if (!registered.get() && registered.compareAndSet(false, true)) {
    Runtime.getRuntime().addShutdownHook(getDubboShutdownHook());
  }
}

代码示例来源: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: apache/incubator-dubbo

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

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

/**
 * Register the ShutdownHook
 */
public void register() {
  if (!registered.get() && registered.compareAndSet(false, true)) {
    Runtime.getRuntime().addShutdownHook(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: spring-projects/spring-framework

/**
 * Create a new {@code TaskExecutorRegistration} for a default
 * {@link ThreadPoolTaskExecutor}.
 */
public TaskExecutorRegistration() {
  this.taskExecutor = new ThreadPoolTaskExecutor();
  this.taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
  this.taskExecutor.setAllowCoreThreadTimeOut(true);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    i++;
    if (i % 100000 == 0) {
      System.out.println(i + "  Total Memory: " + Runtime.getRuntime().totalMemory() + "  Free: " + Runtime.getRuntime().freeMemory());
    }
    if (i < 1000000L) {
      inner.schedule(this);
    } else {
      latch.countDown();
    }
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void run() {
    i++;
    if (i % 100000 == 0) {
      System.out.println(i + "  Total Memory: " + Runtime.getRuntime().totalMemory() + "  Free: " + Runtime.getRuntime().freeMemory());
    }
    if (i < 1000000L) {
      inner.schedule(this);
    } else {
      latch.countDown();
    }
  }
});

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

@Bean(name = {"messageBrokerTaskScheduler", "messageBrokerSockJsTaskScheduler"})
public ThreadPoolTaskScheduler messageBrokerTaskScheduler() {
  ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
  scheduler.setThreadNamePrefix("MessageBroker-");
  scheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
  scheduler.setRemoveOnCancelPolicy(true);
  return scheduler;
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Take a Publisher and prepare to consume it on multiple 'rails' (number of CPUs)
 * in a round-robin fashion.
 * @param <T> the value type
 * @param source the source Publisher
 * @return the ParallelFlowable instance
 */
@CheckReturnValue
public static <T> ParallelFlowable<T> from(@NonNull Publisher<? extends T> source) {
  return from(source, Runtime.getRuntime().availableProcessors(), Flowable.bufferSize());
}

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

@Nullable
private RootBeanDefinition getDefaultExecutorBeanDefinition(String channelName) {
  if (channelName.equals("brokerChannel")) {
    return null;
  }
  RootBeanDefinition executorDef = new RootBeanDefinition(ThreadPoolTaskExecutor.class);
  executorDef.getPropertyValues().add("corePoolSize", Runtime.getRuntime().availableProcessors() * 2);
  executorDef.getPropertyValues().add("maxPoolSize", Integer.MAX_VALUE);
  executorDef.getPropertyValues().add("queueCapacity", Integer.MAX_VALUE);
  executorDef.getPropertyValues().add("allowCoreThreadTimeOut", true);
  return executorDef;
}

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

@Test  // SPR-11623
public void customChannelsWithDefaultExecutor() {
  loadBeanDefinitions("websocket-config-broker-customchannels-default-executor.xml");
  testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
  testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
  assertFalse(this.appContext.containsBean("brokerChannelExecutor"));
}

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

@Test
public void taskScheduler() {
  ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
  String name = "messageBrokerSockJsTaskScheduler";
  ThreadPoolTaskScheduler taskScheduler = config.getBean(name, ThreadPoolTaskScheduler.class);
  ScheduledThreadPoolExecutor executor = taskScheduler.getScheduledThreadPoolExecutor();
  assertEquals(Runtime.getRuntime().availableProcessors(), executor.getCorePoolSize());
  assertTrue(executor.getRemoveOnCancelPolicy());
  SimpleBrokerMessageHandler handler = config.getBean(SimpleBrokerMessageHandler.class);
  assertNotNull(handler.getTaskScheduler());
  assertArrayEquals(new long[] {15000, 15000}, handler.getHeartbeatValue());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void reduceFull() {
  for (int i = 1; i <= Runtime.getRuntime().availableProcessors() * 2; i++) {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Flowable.range(1, 10)
    .parallel(i)
    .reduce(new BiFunction<Integer, Integer, Integer>() {
      @Override
      public Integer apply(Integer a, Integer b) throws Exception {
        return a + b;
      }
    })
    .subscribe(ts);
    ts.assertResult(55);
  }
}

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

@Before
public void setup() {
  this.executor = new ThreadPoolTaskExecutor();
  this.executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
  this.executor.setAllowCoreThreadTimeOut(true);
  this.executor.afterPropertiesSet();
  this.channel = new ExecutorSubscribableChannel(this.executor);
  OrderedMessageSender.configureOutboundChannel(this.channel, true);
  this.sender = new OrderedMessageSender(this.channel, logger);
}

相关文章