Search in sources :

Example 16 with RejectedExecutionHandler

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

Example 17 with RejectedExecutionHandler

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

Example 18 with RejectedExecutionHandler

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;
    }
}
Also used : SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) RejectableScheduledThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableScheduledThreadPoolExecutor) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) RejectableScheduledThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) RejectableThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableThreadPoolExecutor) RejectableScheduledThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableScheduledThreadPoolExecutor)

Example 19 with RejectedExecutionHandler

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);
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) NamedThreadFactory(com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 20 with RejectedExecutionHandler

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();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Test(org.junit.Test)

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