use of java.util.concurrent.RejectedExecutionHandler in project hbase by apache.
the class TestMobCompactor method createThreadPool.
private static ExecutorService createThreadPool(Configuration conf) {
int maxThreads = 10;
long keepAliveTime = 60;
final SynchronousQueue<Runnable> queue = new SynchronousQueue<>();
ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS, queue, Threads.newDaemonThreadFactory("MobFileCompactionChore"), new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
// waiting for a thread to pick up instead of throwing exceptions.
queue.put(r);
} catch (InterruptedException e) {
throw new RejectedExecutionException(e);
}
}
});
((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
return pool;
}
use of java.util.concurrent.RejectedExecutionHandler in project bazel by bazelbuild.
the class ExecutorUtil method newSlackPool.
/**
* Create a "slack" thread pool which has the following properties:
* 1. the worker count shrinks as the threads go unused
* 2. the rejection policy is caller-runs
*
* @param threads maximum number of threads in the pool
* @param name name of the pool
* @return the new ThreadPoolExecutor
*/
public static ThreadPoolExecutor newSlackPool(int threads, String name) {
// Using a synchronous queue with a bounded thread pool means we'll reject
// tasks after the pool size. The CallerRunsPolicy, however, implies that
// saturation is handled in the calling thread.
ThreadPoolExecutor pool = new ThreadPoolExecutor(threads, threads, 3L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
// Do not consume threads when not in use.
pool.allowCoreThreadTimeOut(true);
pool.setThreadFactory(new ThreadFactoryBuilder().setNameFormat(name + " %d").build());
pool.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
r.run();
}
});
return pool;
}
use of java.util.concurrent.RejectedExecutionHandler in project camel by apache.
the class DefaultThreadPoolFactory method newScheduledThreadPool.
@Override
public ScheduledExecutorService newScheduledThreadPool(ThreadPoolProfile profile, ThreadFactory threadFactory) {
RejectedExecutionHandler rejectedExecutionHandler = profile.getRejectedExecutionHandler();
if (rejectedExecutionHandler == null) {
rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
}
ScheduledThreadPoolExecutor answer = new RejectableScheduledThreadPoolExecutor(profile.getPoolSize(), threadFactory, rejectedExecutionHandler);
answer.setRemoveOnCancelPolicy(true);
// we could potentially keep adding tasks, and run out of memory.
if (profile.getMaxPoolSize() > 0) {
return new SizedScheduledExecutorService(answer, profile.getMaxQueueSize());
} else {
return answer;
}
}
use of java.util.concurrent.RejectedExecutionHandler in project otter by alibaba.
the class DefaultCommunicationClientImpl method initial.
public void initial() {
RejectedExecutionHandler handler = null;
if (discard) {
handler = new ThreadPoolExecutor.DiscardPolicy();
} else {
handler = new ThreadPoolExecutor.AbortPolicy();
}
executor = new ThreadPoolExecutor(poolSize, poolSize, 60 * 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10 * 1000), new NamedThreadFactory("communication-async"), handler);
}
use of java.util.concurrent.RejectedExecutionHandler in project spring-framework by spring-projects.
the class ScheduledExecutorFactoryBeanTests method testShutdownNowIsPropagatedToTheExecutorOnDestroy.
@Test
@SuppressWarnings("serial")
public void testShutdownNowIsPropagatedToTheExecutorOnDestroy() throws Exception {
final ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
@Override
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
return executor;
}
};
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[] { new NoOpScheduledExecutorTask() });
factory.afterPropertiesSet();
factory.destroy();
verify(executor).shutdownNow();
}
Aggregations