/** * 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; }
/** * 开启多线程 * @param enable true表示开启[系统CPU核心数]个线程,false表示单线程 * @return */ public Segment enableMultithreading(boolean enable) { if (enable) config.threadNumber = Runtime.getRuntime().availableProcessors(); else config.threadNumber = 1; return this; }
int cores = Runtime.getRuntime().availableProcessors();
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CORE_POOL_SIZE = CPU_COUNT + 1; private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(128);
public LRUCacheMap(int size, long timeToLiveInMillis, long maxIdleInMillis) { super(size, timeToLiveInMillis, maxIdleInMillis); for (int i = 0; i < Runtime.getRuntime().availableProcessors()*2; i++) { Set<CachedValue<K, V>> instance = Collections.synchronizedSet(new LinkedHashSet<CachedValue<K, V>>()); queues.add(instance); } }
/** * Determines the number of cores available on the device. */ static int availableProcessors() { int cpus = Runtime.getRuntime().availableProcessors(); if (Build.VERSION.SDK_INT < 17) { cpus = Math.max(getCoreCountPre17(), cpus); } return cpus; }
@Override public Result train(String trainingFile, String developFile, String modelFile) throws IOException { // 词性标注模型压缩会显著降低效果 return train(trainingFile, developFile, modelFile, 0, 10, Runtime.getRuntime().availableProcessors()); } }
public Result train(String trainingFile, String developFile, String modelFile) throws IOException { return train(trainingFile, developFile, modelFile, 0.1, 50, Runtime.getRuntime().availableProcessors()); }
@Inject public IndexLookup(Connector connector, ColumnCardinalityCache cardinalityCache) { this.connector = requireNonNull(connector, "connector is null"); this.cardinalityCache = requireNonNull(cardinalityCache, "cardinalityCache is null"); // Create a bounded executor with a pool size at 4x number of processors this.coreExecutor = newCachedThreadPool(daemonThreadsNamed("cardinality-lookup-%s")); this.executorService = new BoundedExecutor(coreExecutor, 4 * Runtime.getRuntime().availableProcessors()); }
/** * 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); }
@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()); }
public static RuntimeBeanReference registerScheduler( String schedulerName, ParserContext context, @Nullable Object source) { if (!context.getRegistry().containsBeanDefinition(schedulerName)) { RootBeanDefinition taskSchedulerDef = new RootBeanDefinition(ThreadPoolTaskScheduler.class); taskSchedulerDef.setSource(source); taskSchedulerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); taskSchedulerDef.getPropertyValues().add("poolSize", Runtime.getRuntime().availableProcessors()); taskSchedulerDef.getPropertyValues().add("threadNamePrefix", schedulerName + "-"); taskSchedulerDef.getPropertyValues().add("removeOnCancelPolicy", true); context.getRegistry().registerBeanDefinition(schedulerName, taskSchedulerDef); context.registerComponent(new BeanComponentDefinition(taskSchedulerDef, schedulerName)); } return new RuntimeBeanReference(schedulerName); }
@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; }
/** * 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()); }
@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 threadPoolSizeDefault() { ApplicationContext context = loadConfig(DefaultConfig.class); String name = "clientInboundChannelExecutor"; ThreadPoolTaskExecutor executor = context.getBean(name, ThreadPoolTaskExecutor.class); assertEquals(Runtime.getRuntime().availableProcessors() * 2, executor.getCorePoolSize()); // No way to verify queue capacity name = "clientOutboundChannelExecutor"; executor = context.getBean(name, ThreadPoolTaskExecutor.class); assertEquals(Runtime.getRuntime().availableProcessors() * 2, executor.getCorePoolSize()); name = "brokerChannelExecutor"; executor = context.getBean(name, ThreadPoolTaskExecutor.class); assertEquals(0, executor.getCorePoolSize()); assertEquals(1, executor.getMaxPoolSize()); }
@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); }