@VisibleForTesting void addShutdownHook(Thread hook) { Runtime.getRuntime().addShutdownHook(hook); } }
/** * 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; }
/** * 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(); } }
/** * Unregister the ShutdownHook */ public void unregister() { if (registered.get() && registered.compareAndSet(true, false)) { Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook()); } }
@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); }
/** * Register the ShutdownHook */ public void register() { if (!registered.get() && registered.compareAndSet(false, true)) { Runtime.getRuntime().addShutdownHook(getDubboShutdownHook()); } }
@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); }
/** * Unregister the ShutdownHook */ public void unregister() { if (registered.get() && registered.compareAndSet(true, false)) { Runtime.getRuntime().removeShutdownHook(getDubboShutdownHook()); } }
/** * Register the ShutdownHook */ public void register() { if (!registered.get() && registered.compareAndSet(false, true)) { Runtime.getRuntime().addShutdownHook(getDubboShutdownHook()); } }
@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(); }
/** * 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); }
@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(); } } });
@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(); } } });
@Bean(name = {"messageBrokerTaskScheduler", "messageBrokerSockJsTaskScheduler"}) public ThreadPoolTaskScheduler messageBrokerTaskScheduler() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.setThreadNamePrefix("MessageBroker-"); scheduler.setPoolSize(Runtime.getRuntime().availableProcessors()); scheduler.setRemoveOnCancelPolicy(true); return scheduler; }
/** * 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()); }
@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; }
@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")); }
@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()); }
@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); } }
@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); }