use of java.util.concurrent.RejectedExecutionHandler in project hadoop by apache.
the class BlockingThreadPoolExecutorService method newInstance.
/**
* A thread pool that that blocks clients submitting additional tasks if
* there are already {@code activeTasks} running threads and {@code
* waitingTasks} tasks waiting in its queue.
*
* @param activeTasks maximum number of active tasks
* @param waitingTasks maximum number of waiting tasks
* @param keepAliveTime time until threads are cleaned up in {@code unit}
* @param unit time unit
* @param prefixName prefix of name for threads
*/
public static BlockingThreadPoolExecutorService newInstance(int activeTasks, int waitingTasks, long keepAliveTime, TimeUnit unit, String prefixName) {
/* Although we generally only expect up to waitingTasks tasks in the
queue, we need to be able to buffer all tasks in case dequeueing is
slower than enqueueing. */
final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(waitingTasks + activeTasks);
ThreadPoolExecutor eventProcessingExecutor = new ThreadPoolExecutor(activeTasks, activeTasks, keepAliveTime, unit, workQueue, newDaemonThreadFactory(prefixName), new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// This is not expected to happen.
LOG.error("Could not submit task to executor {}", executor.toString());
}
});
eventProcessingExecutor.allowCoreThreadTimeOut(true);
return new BlockingThreadPoolExecutorService(waitingTasks + activeTasks, eventProcessingExecutor);
}
use of java.util.concurrent.RejectedExecutionHandler in project hbase by apache.
the class TestPartitionedMobCompactor method createThreadPool.
private static ExecutorService createThreadPool() {
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 crate by crate.
the class NodeThreadPoolExpression method addChildImplementations.
private void addChildImplementations() {
childImplementations.put(POOL_NAME, new LiteralReferenceImplementation<>(name));
childImplementations.put(ACTIVE, threadPoolExecutor::getActiveCount);
childImplementations.put(REJECTED, () -> {
long rejected = -1;
RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor.getRejectedExecutionHandler();
if (rejectedExecutionHandler instanceof XRejectedExecutionHandler) {
rejected = ((XRejectedExecutionHandler) rejectedExecutionHandler).rejected();
}
return rejected;
});
childImplementations.put(LARGEST, threadPoolExecutor::getLargestPoolSize);
childImplementations.put(COMPLETED, threadPoolExecutor::getCompletedTaskCount);
childImplementations.put(THREADS, threadPoolExecutor::getPoolSize);
childImplementations.put(QUEUE, () -> threadPoolExecutor.getQueue().size());
}
use of java.util.concurrent.RejectedExecutionHandler in project elasticsearch by elastic.
the class ThreadPool method stats.
public ThreadPoolStats stats() {
List<ThreadPoolStats.Stats> stats = new ArrayList<>();
for (ExecutorHolder holder : executors.values()) {
String name = holder.info.getName();
// no need to have info on "same" thread pool
if ("same".equals(name)) {
continue;
}
int threads = -1;
int queue = -1;
int active = -1;
long rejected = -1;
int largest = -1;
long completed = -1;
if (holder.executor() instanceof ThreadPoolExecutor) {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) holder.executor();
threads = threadPoolExecutor.getPoolSize();
queue = threadPoolExecutor.getQueue().size();
active = threadPoolExecutor.getActiveCount();
largest = threadPoolExecutor.getLargestPoolSize();
completed = threadPoolExecutor.getCompletedTaskCount();
RejectedExecutionHandler rejectedExecutionHandler = threadPoolExecutor.getRejectedExecutionHandler();
if (rejectedExecutionHandler instanceof XRejectedExecutionHandler) {
rejected = ((XRejectedExecutionHandler) rejectedExecutionHandler).rejected();
}
}
stats.add(new ThreadPoolStats.Stats(name, threads, queue, active, rejected, largest, completed));
}
return new ThreadPoolStats(stats);
}
use of java.util.concurrent.RejectedExecutionHandler in project neo4j by neo4j.
the class BatchingMultipleIndexPopulator method createThreadPool.
private ExecutorService createThreadPool() {
int threads = getNumberOfPopulationWorkers();
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(TASK_QUEUE_SIZE);
ThreadFactory threadFactory = daemon(FLUSH_THREAD_NAME_PREFIX);
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
return new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, workQueue, threadFactory, rejectedExecutionHandler);
}
Aggregations