Example usage for java.util.concurrent ThreadPoolExecutor setRejectedExecutionHandler

List of usage examples for java.util.concurrent ThreadPoolExecutor setRejectedExecutionHandler

Introduction

In this page you can find the example usage for java.util.concurrent ThreadPoolExecutor setRejectedExecutionHandler.

Prototype

public void setRejectedExecutionHandler(RejectedExecutionHandler handler) 

Source Link

Document

Sets a new handler for unexecutable tasks.

Usage

From source file:org.mule.config.pool.DefaultThreadPoolFactory.java

private void configureThreadPoolExecutor(String name, ThreadingProfile tp, ThreadPoolExecutor pool) {
    configureThreadFactory(name, tp, pool);

    if (tp.getRejectedExecutionHandler() != null) {
        pool.setRejectedExecutionHandler(tp.getRejectedExecutionHandler());
    } else {//from ww  w .j ava  2 s  . c o m
        switch (tp.getPoolExhaustedAction()) {
        case ThreadingProfile.WHEN_EXHAUSTED_DISCARD_OLDEST:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
            break;
        case ThreadingProfile.WHEN_EXHAUSTED_RUN:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            break;
        case ThreadingProfile.WHEN_EXHAUSTED_ABORT:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
            break;
        case ThreadingProfile.WHEN_EXHAUSTED_DISCARD:
            pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
            break;
        default:
            // WHEN_EXHAUSTED_WAIT
            pool.setRejectedExecutionHandler(new WaitPolicy(tp.getThreadWaitTimeout(), TimeUnit.MILLISECONDS));
            break;
        }
    }
}

From source file:org.openspaces.admin.internal.admin.DefaultAdmin.java

private ThreadPoolExecutor createThreadPoolExecutor(String threadName, int numberOfThreads,
        final boolean updateSingleThreadId) {
    final ClassLoader correctClassLoader = Thread.currentThread().getContextClassLoader();
    ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(numberOfThreads,
            new GSThreadFactory(threadName, useDaemonThreads) {

                @Override/*w w  w.j ava  2  s .  co m*/
                public Thread newThread(Runnable r) {
                    Thread thread = super.newThread(r);
                    thread.setContextClassLoader(correctClassLoader);
                    if (updateSingleThreadId) {
                        DefaultAdmin.this.executorSingleThreadId = thread.getId();
                    }
                    return thread;
                }
            });
    executorService.setRejectedExecutionHandler(DEFAULT_EVENT_LISTENER_REJECTED_POLICY);
    return executorService;
}

From source file:org.polymap.core.runtime.UnboundPoolExecutor.java

public static ExecutorService newInstance() {
    final int procs = Runtime.getRuntime().availableProcessors();
    final int maxThreads = procs * MAX_THREADS_PER_PROC;

    // thread factory
    ThreadFactory threadFactory = new ThreadFactory() {
        volatile int threadNumber = 0;

        public Thread newThread(Runnable r) {
            String prefix = "polymap-";
            Thread t = new Thread(r, prefix + threadNumber++);
            t.setDaemon(false);//w w  w  .  j a  v a 2  s.c o  m
            t.setPriority(DEFAULT_THREAD_PRIORITY);
            return t;
        }
    };

    // thread pool
    ThreadPoolExecutor executor = new ThreadPoolExecutor(procs, maxThreads, 180L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), threadFactory);

    // rejected? -> wait and try again
    executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        Random rand = new Random();

        public void rejectedExecution(Runnable r, ThreadPoolExecutor _executor) {
            do {
                try {
                    Thread.sleep(rand.nextInt(1000) + 100);
                } catch (InterruptedException e) {
                }
            } while (_executor.getActiveCount() >= maxThreads);

            _executor.execute(r);
        }
    });

    //executor.allowCoreThreadTimeOut( true );        
    return executor;
}