Search in sources :

Example 1 with RejectedExecutionHandler

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);
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 2 with RejectedExecutionHandler

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;
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with RejectedExecutionHandler

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());
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) XRejectedExecutionHandler(org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler) XRejectedExecutionHandler(org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler)

Example 4 with RejectedExecutionHandler

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);
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) XRejectedExecutionHandler(org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler) XRejectedExecutionHandler(org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler) ArrayList(java.util.ArrayList) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) EsThreadPoolExecutor(org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 5 with RejectedExecutionHandler

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);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)23 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 ThreadFactory (java.util.concurrent.ThreadFactory)6 ExecutorService (java.util.concurrent.ExecutorService)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Metacard (ddf.catalog.data.Metacard)2 SourceResponse (ddf.catalog.operation.SourceResponse)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 XRejectedExecutionHandler (org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler)2 Test (org.junit.Test)2 NamedThreadFactory (com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory)1