ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor( pullMsgThreadSize, new BasicThreadFactory.Builder() .namingPattern( "PullMessageTask-" + messageType + "-thread-pool-%d") .daemon(true).build()); executorService = scheduledExecutorService;
/** * Tests the reset() method of the builder. */ @Test public void testBuilderReset() { final ThreadFactory wrappedFactory = EasyMock.createMock(ThreadFactory.class); final Thread.UncaughtExceptionHandler exHandler = EasyMock .createMock(Thread.UncaughtExceptionHandler.class); EasyMock.replay(wrappedFactory, exHandler); builder.namingPattern(PATTERN).daemon(true).priority( Thread.MAX_PRIORITY).uncaughtExceptionHandler(exHandler) .wrappedFactory(wrappedFactory); builder.reset(); final BasicThreadFactory factory = builder.build(); checkFactoryDefaults(factory); assertNotSame("Wrapped factory not reset", wrappedFactory, factory .getWrappedFactory()); EasyMock.verify(wrappedFactory, exHandler); }
logger.info("starting appConnectionCache"); logger.info("start caching connections"); ;; BasicThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("appconnectioncache-scheduler-thread-%d").build(); scheduler = Executors.newScheduledThreadPool(1, factory);
final boolean noExit = Boolean.parseBoolean(options.getOrDefault("noExit", "false").toString()); final int parallelism = Integer.parseInt(options.getOrDefault("parallelism", "16").toString()); final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("profiler-%d").build(); final ExecutorService executor = Executors.newFixedThreadPool(parallelism, threadFactory);
BasicThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("thread-%d").build(); executorService = Executors.newSingleThreadExecutor(factory);
new BasicThreadFactory.Builder().namingPattern("gremlin-driver-worker-%d").build()); this.executor.setRemoveOnCancelPolicy(true);
log.info("Server restart is not enabled. Not persisting lens server state"); } else { ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("Lens-server-snapshotter-Thread-%d") .daemon(true) .priority(Thread.NORM_PRIORITY) .build(); serverSnapshotScheduler = Executors.newSingleThreadScheduledExecutor(factory); serverSnapshotScheduler.scheduleWithFixedDelay(new Runnable() {
final CyclicBarrier barrier = new CyclicBarrier(runs); ExecutorService executor = Executors.newCachedThreadPool( new BasicThreadFactory.Builder() .uncaughtExceptionHandler((t, e) -> log.log(Level.SEVERE, "An uncaught exception happened in Thread " + t.getName(), e)) .namingPattern(ThreadTestHelper.class.getSimpleName() + "-Thread-%d") .build()); try { List<Callable<T>> tasks = new ArrayList<>(runs);
final ThreadFactory threadFactory = new BasicThreadFactory.Builder() .daemon(true).namingPattern(getClass().getSimpleName() + " Push thread").build();
public WebSocketClient(final URI uri) { final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("ws-client-%d").build(); group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), threadFactory); final Bootstrap b = new Bootstrap().group(group);
new BasicThreadFactory.Builder().namingPattern("SendNodeMessage-Fail-Retry-schedule-pool-%d").daemon(true).build()); scheduledExecutorService.scheduleAtFixedRate(() ->
@PostConstruct public void start() { LinkedBlockingQueue<Runnable> executorQueue = new LinkedBlockingQueue<>(1000); BasicThreadFactory threadFactory = new BasicThreadFactory.Builder() .namingPattern("SubscriptionTriggering-%d") .daemon(false) .priority(Thread.NORM_PRIORITY) .build(); RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() { @Override
LensConfConstants.DEFAULT_KDC_LOGIN_SERVICE_INTERVAL_IN_MINUTES); ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("Lens-server-refresh-tgt-Thread-%d") .daemon(true) .priority(Thread.NORM_PRIORITY) .build(); kerberosTgtScheduler = Executors.newSingleThreadScheduledExecutor(factory); kerberosTgtScheduler.scheduleWithFixedDelay(new Runnable() {
/** * Creates a Controller with the set of listener properties which will be used to start listener threads. * * @param properties * A non null Set of ListenerProperties */ public ControllerImpl(Set<ListenerProperties> properties) { listeners = new HashMap<ListenerProperties, Listener>(); for (ListenerProperties props : properties) { if (props.getClass() != null) { listeners.put(props, null); } else { LOG.error(String.format( "The ListenerProperties %s has no Listener class associated with it and will not run.", props)); properties.remove(props); } } LISTENER_COUNT = properties.size(); // Only create executor if listeners are configured if (LISTENER_COUNT > 0) { executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(LISTENER_COUNT)); // Custom Named thread factory BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build(); executor.setThreadFactory(threadFactory); } }
@Override public ThreadPoolExecutor createValue(String key) { int coreSize = configurationsResolver.getProperty(configurationsCommonPrefix + key + ".coreSize", Integer.class, defaultCoreSize); int maxSize = configurationsResolver.getProperty(configurationsCommonPrefix + key + ".maxSize", Integer.class, defaultMaxSize); long keepAliveSeconds = configurationsResolver.getProperty(configurationsCommonPrefix + key + ".keepAliveSeconds", Long.class, defaultKeepAliveSeconds); int queueSize = configurationsResolver.getProperty(configurationsCommonPrefix + key + ".queueSize", Integer.class, defaultQueueSize); boolean allowCoreThreadTimeout = configurationsResolver.getProperty(configurationsCommonPrefix + key + ".allowCoreThreadTimeout", Boolean.class, defaultAllowCoreThreadTimeout); ThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern(key + "-%04d").build(); ThreadPoolExecutor pool; if (key.contains(KEYWORD_SCHEDULED) || key.contains(KEYWORD_SCHD)){ ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(coreSize, threadFactory); setRemoveOnCancelPolicy(p, true); pool = p; logger.debug("Created scheduled thread pool '{}': size={}",key, coreSize); }else{ pool = new ThreadPoolExecutor(coreSize, maxSize, keepAliveSeconds, TimeUnit.SECONDS, queueSize < 10000000 ? new ArrayBlockingQueue<Runnable>(queueSize) : new LinkedBlockingQueue<Runnable>(queueSize), threadFactory); pool.allowCoreThreadTimeOut(allowCoreThreadTimeout); logger.debug("Created thread pool '{}': coreSize={}, maxSize={}, keepAliveSeconds={}, queueSize={}, allowCoreThreadTimeout={}", key, coreSize, maxSize, keepAliveSeconds, queueSize, allowCoreThreadTimeout); } return pool; } });
@PostConstruct public void init() { BasicThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("myspringbean-thread-%d").build(); executorService = Executors.newSingleThreadExecutor(factory); executorService.execute(new Runnable() { @Override public void run() { try { // do something System.out.println("thread started"); } catch (Exception e) { logger.error("error: ", e); } } }); executorService.shutdown(); } @PreDestroy public void beandestroy() { if(executorService != null){ executorService.shutdownNow(); } }
@PostConstruct public void init() { scheduledExecutorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("SendNodeServerInfo-schedule-pool-%d").daemon(true).build()); scheduledExecutorService.scheduleAtFixedRate(() -> { //将负载加载到ZK中 if (!CollectionUtils.isEmpty(dataCenterChannelStore.getAllChannels())) { dataCenterChannelStore.getAllChannels().stream().forEach(e -> { log.info("channel id:{}, {}", e.id(), e); }); } applicationEventPublisher.publishEvent( NodeServerInfoEvent.builder() .name(goPushNodeServerConfig.getName()) .nodeServerInfo(watch()) .build()); // 写入zk 其实不需要发送 NodeInfoReq nodeSender.send(NodeInfoReq.builder().build()); } , delay, delay, TimeUnit.MILLISECONDS); }
public NioClient(final URI uri) { final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("nio-client-%d").build(); group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), threadFactory); final Bootstrap b = new Bootstrap().group(group); try { final MessageSerializer serializer = new KryoMessageSerializerV1d0(); b.channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel ch) { final ChannelPipeline p = ch.pipeline(); p.addLast( new NioGremlinResponseDecoder(serializer), new NioGremlinRequestEncoder(true, serializer), callbackResponseHandler); } }); channel = b.connect(uri.getHost(), uri.getPort()).sync().channel(); } catch (Exception ex) { throw new RuntimeException(ex); } }
/** * Create an asynchronous event listener which uses a thread poool to process events. * * @param poolSize size of the event processing pool * @param timeOutSeconds time out in seconds when an idle thread is destroyed * @param isDaemon if the threads used to process should be daemon threads, * if false, then implementation should call stop() * to stop the thread pool */ public AsyncEventListener(int poolSize, long timeOutSeconds, final boolean isDaemon) { eventQueue = new LinkedBlockingQueue<>(); ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern(getName()+"_AsyncThread-%d") .daemon(isDaemon) .priority(Thread.NORM_PRIORITY) .build(); // fixed pool with min and max equal to poolSize processor = new ThreadPoolExecutor(poolSize, poolSize, timeOutSeconds, TimeUnit.SECONDS, eventQueue, factory); processor.allowCoreThreadTimeOut(true); }
public GremlinExecutor create() { final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-executor-default-%d").build(); final AtomicBoolean poolCreatedByBuilder = new AtomicBoolean(); final AtomicBoolean suppliedExecutor = new AtomicBoolean(true); final AtomicBoolean suppliedScheduledExecutor = new AtomicBoolean(true); final ExecutorService es = Optional.ofNullable(executorService).orElseGet(() -> { poolCreatedByBuilder.set(true); suppliedExecutor.set(false); return Executors.newScheduledThreadPool(4, threadFactory); }); executorService = es; final ScheduledExecutorService ses = Optional.ofNullable(scheduledExecutorService).orElseGet(() -> { // if the pool is created by the builder and we need another just re-use it, otherwise create // a new one of those guys suppliedScheduledExecutor.set(false); return (poolCreatedByBuilder.get()) ? (ScheduledExecutorService) es : Executors.newScheduledThreadPool(4, threadFactory); }); scheduledExecutorService = ses; return new GremlinExecutor(this, suppliedExecutor.get(), suppliedScheduledExecutor.get()); } }