Java Code Examples for java.util.concurrent.ScheduledThreadPoolExecutor#setRejectedExecutionHandler()

The following examples show how to use java.util.concurrent.ScheduledThreadPoolExecutor#setRejectedExecutionHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("SameParameterValue")
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
                                                                         int corePoolSize,
                                                                         int maximumPoolSize,
                                                                         long keepAliveTimeInSec) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder()
            .setNameFormat(name)
            .setDaemon(true)
            .setPriority(Thread.MIN_PRIORITY)
            .build();
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    executor.setKeepAliveTime(keepAliveTimeInSec, TimeUnit.SECONDS);
    executor.allowCoreThreadTimeOut(true);
    executor.setMaximumPoolSize(maximumPoolSize);
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setRejectedExecutionHandler((r, e) -> log.debug("RejectedExecutionHandler called"));
    return executor;
}
 
Example 2
Source File: ThreadPoolManager.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
private ThreadPoolManager() {
	final int instantPoolSize = Math.max(1, ThreadConfig.BASE_THREAD_POOL_SIZE) * Runtime.getRuntime().availableProcessors();

	instantPool = new ThreadPoolExecutor(instantPoolSize, instantPoolSize, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100000), new PriorityThreadFactory("InstantPool", ThreadConfig.USE_PRIORITIES ? 7 : Thread.NORM_PRIORITY));
	instantPool.setRejectedExecutionHandler(new AionRejectedExecutionHandler());
	instantPool.prestartAllCoreThreads();

	scheduledPool = new ScheduledThreadPoolExecutor(Math.max(1, ThreadConfig.EXTRA_THREAD_PER_CORE) * Runtime.getRuntime().availableProcessors());
	scheduledPool.setRejectedExecutionHandler(new AionRejectedExecutionHandler());
	scheduledPool.prestartAllCoreThreads();

	longRunningPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
	longRunningPool.setRejectedExecutionHandler(new AionRejectedExecutionHandler());
	longRunningPool.prestartAllCoreThreads();

	WorkStealThreadFactory forkJoinThreadFactory = new WorkStealThreadFactory("ForkJoinPool");
	workStealingPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), forkJoinThreadFactory, new ThreadUncaughtExceptionHandler(), true);
	forkJoinThreadFactory.setDefaultPool(workStealingPool);

	Thread maintainThread = new Thread(new Runnable() {

		@Override
		public void run() {
			purge();
		}
	}, "ThreadPool Purge Task");

	maintainThread.setDaemon(true);
	scheduleAtFixedRate(maintainThread, 150000, 150000);

	log.info("ThreadPoolManager: Initialized with " + scheduledPool.getPoolSize() + " scheduler, " + instantPool.getPoolSize() + " instant, " + longRunningPool.getPoolSize() + " long running, and forking " + workStealingPool.getPoolSize() + " thread(s).");
}
 
Example 3
Source File: CustomSchedulingConfiguration.java    From booties with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "shutdown")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ScheduledExecutorService scheduledExecutorService() {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(properties.getCorePoolSize());

    executor.setMaximumPoolSize(properties.getMaxPoolSize());

    executor.setThreadFactory(new CustomizableThreadFactory(properties.getThreadNamePrefix()));

    executor.setRejectedExecutionHandler(getConfiguredRejectedExecutionHandler());
    return executor;
}
 
Example 4
Source File: ServiceQueue.java    From asteria-3.0 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates and configures a new {@link ScheduledExecutorService} with a
 * timeout value of {@code seconds}. If the timeout value is below or equal
 * to zero then the returned executor will never timeout.
 *
 * @return the newly created and configured executor service.
 */
private static ScheduledExecutorService createServiceExecutor(long seconds) {
    Preconditions.checkArgument(seconds >= 0, "The timeout value must be equal to or greater than 0!");
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.setRejectedExecutionHandler(new CallerRunsPolicy());
    executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("ServiceQueueThread").build());
    if (seconds > 0) {
        executor.setKeepAliveTime(seconds, TimeUnit.SECONDS);
        executor.allowCoreThreadTimeOut(true);
    }
    return executor;
}