Search in sources :

Example 6 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project spring-framework by spring-projects.

the class ScheduledExecutorFactoryBeanTests method testShutdownIsPropagatedToTheExecutorOnDestroy.

@Test
@SuppressWarnings("serial")
public void testShutdownIsPropagatedToTheExecutorOnDestroy() 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.setWaitForTasksToCompleteOnShutdown(true);
    factory.afterPropertiesSet();
    factory.destroy();
    verify(executor).shutdown();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Test(org.junit.Test)

Example 7 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project hazelcast by hazelcast.

the class LoggingScheduledExecutorTest method testConstructor_withRejectedExecutionHandler.

@Test
public void testConstructor_withRejectedExecutionHandler() {
    RejectedExecutionHandler handler = new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
        }
    };
    executor = new LoggingScheduledExecutor(logger, 1, factory, handler);
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project yyl_example by Relucent.

the class ThreadPoolExecutorTest2 method main.

public static void main(String[] args) throws InterruptedException {
    int cpu = Runtime.getRuntime().availableProcessors();
    ThreadPoolExecutor executor = new //
    ThreadPoolExecutor(//
    0, //
    cpu * 2, //
    60, //
    TimeUnit.SECONDS, //
    new SynchronousQueue<Runnable>(), // 自定义实现,使用当前线程继续执行任务
    new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
            if (!executor.isShutdown()) {
                runnable.run();
            }
        }
    });
    for (int i = 0; i < 100; i++) {
        executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Thread.sleep(1 * 1000);
                System.out.println(Thread.currentThread().getName());
                return null;
            }
        });
    }
    executor.awaitTermination(1 * 1000 * 100, TimeUnit.SECONDS);
    executor.shutdown();
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 9 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project apex-core by apache.

the class DefaultApexPluginDispatcher method serviceInit.

@Override
protected void serviceInit(Configuration conf) throws Exception {
    super.serviceInit(conf);
    LOG.debug("Creating plugin dispatch queue with size {}", qsize);
    blockingQueue = new ArrayBlockingQueue<>(qsize);
    RejectedExecutionHandler rejectionHandler = new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                blockingQueue.remove();
                executor.submit(r);
            } catch (NoSuchElementException ex) {
            // Ignore no-such element as queue may finish, while this handler is called.
            }
        }
    };
    executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, blockingQueue, new NameableThreadFactory("PluginExecutorThread"), rejectionHandler);
}
Also used : RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) NameableThreadFactory(com.datatorrent.common.util.NameableThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project lucene-solr by apache.

the class DocExpirationUpdateProcessorFactory method initDeleteExpiredDocsScheduler.

private void initDeleteExpiredDocsScheduler(SolrCore core) {
    executor = new ScheduledThreadPoolExecutor(1, new DefaultSolrThreadFactory("autoExpireDocs"), new RejectedExecutionHandler() {

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            log.warn("Skipping execution of '{}' using '{}'", r, e);
        }
    });
    core.addCloseHook(new CloseHook() {

        public void postClose(SolrCore core) {
            if (executor.isTerminating()) {
                log.info("Waiting for close of DocExpiration Executor");
                ExecutorUtil.shutdownAndAwaitTermination(executor);
            }
        }

        public void preClose(SolrCore core) {
            log.info("Triggering Graceful close of DocExpiration Executor");
            executor.shutdown();
        }
    });
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    // we don't want this firing right away, since the core may not be ready
    final long initialDelay = deletePeriodSeconds;
    // TODO: should we make initialDelay configurable
    // TODO: should we make initialDelay some fraction of the period?
    executor.scheduleAtFixedRate(new DeleteExpiredDocsRunnable(this), deletePeriodSeconds, deletePeriodSeconds, TimeUnit.SECONDS);
}
Also used : CloseHook(org.apache.solr.core.CloseHook) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SolrCore(org.apache.solr.core.SolrCore) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

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