Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

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

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters and Executors#defaultThreadFactory default thread factory .

Usage

From source file:Main.java

public static ExecutorService newThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
        TimeUnit unit, BlockingQueue<Runnable> workQueue, String processName, boolean isDaemon) {
    return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
            newThreadFactory(processName, isDaemon));
}

From source file:org.openstreetmap.josm.data.cache.HostLimitQueueTest.java

private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) {
    HostLimitQueue workQueue = new HostLimitQueue(queueLimit);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool
            workers, // do not this number of threads
            300, // keepalive for thread
            TimeUnit.SECONDS, workQueue, Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY));
    workQueue.setExecutor(executor);//from   w  w  w  . j  av a2 s .co m
    return executor;
}

From source file:Main.java

/**
 * Similar to {@link #newCoalescingThreadPool(String)}, but always runs the
 * last runnable put into the pool. If runnable a is executed, and then
 * runnables b and c are executed while a is still running, runnable b will
 * be dropped and c will executed.//from   ww w  . j  ava  2s  . c o m
 */
public static ExecutorService newLastRequestThreadPool(String name) {
    return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>() {
        @Override
        public boolean offer(Runnable e) {
            clear();
            return super.offer(e);
        }
    }, newNamedThreadFactory(name));
}

From source file:Main.java

/**
 * Creates a single thread executor which ignores all executions that occur
 * while it is busy executing a Runnable. This is useful for tasks that may
 * be requested multiple times from multiple sources, but which only need to
 * take place once./*from  w ww.j a  va2s.co  m*/
 * 
 * @param name the name for threads created within this pool
 */
@SuppressWarnings("serial")
public static ExecutorService newCoalescingThreadPool(String name) {
    return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
            newNamedThreadFactory(name)) {
        private boolean executing = false;

        @Override
        public void execute(final Runnable command) {
            synchronized (this) {
                if (executing)
                    return;
                executing = true;
            }
            super.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        command.run();
                    } finally {
                        executing = false;
                    }
                }
            });
        }
    };
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(String name, int numThreads, int maxPoolSize,
        int keepAliveTimeInSeconds) {
    LinkedBlockingQueue<Runnable> lbq = new LinkedBlockingQueue<Runnable>();
    ThreadFactory tf = newNamedThreadFactory(name);
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(numThreads, maxPoolSize, keepAliveTimeInSeconds,
            TimeUnit.SECONDS, lbq, tf);
    return Executors.newFixedThreadPool(numThreads, tpe.getThreadFactory());
}

From source file:Main.java

/**
 * Creates a fixed priority thread pool, sorted with the given comparator.
 *//*from w  w w .j  a  v a2s  . c o  m*/
public static ExecutorService newPriorityThreadPool(String name, int numThreads,
        Comparator<Runnable> comparator) {
    return new ThreadPoolExecutor(numThreads, numThreads, 1, TimeUnit.SECONDS,
            new PriorityBlockingQueue<Runnable>(numThreads, comparator), newNamedThreadFactory(name));
}

From source file:Main.java

public static ExecutorService fixedThreadsExecutor(String name, int count) {
    ThreadFactory threadFactory = daemonThreadFactory(name);

    return new ThreadPoolExecutor(count, count, 10, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), threadFactory) {
        @Override//from   w ww  .j a va 2  s.  c  om
        protected void afterExecute(Runnable runnable, Throwable throwable) {
            if (throwable != null) {
                //Console.getInstance().info("Unexpected failure from " + runnable, throwable);
            }
        }
    };
}

From source file:org.apache.streams.elasticsearch.example.ElasticsearchDelete.java

private static ExecutorService newFixedThreadPoolWithQueueSize(int nThreads, int queueSize) {
    return new ThreadPoolExecutor(nThreads, nThreads, 5000L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(queueSize, true), new ThreadPoolExecutor.CallerRunsPolicy());
}

From source file:edu.cornell.mannlib.ld4lindexing.ThreadPool.java

public ThreadPool(Settings settings) {
    int poolSize = settings.getThreadPoolSize();
    int queueSize = settings.getTaskQueueSize();
    this.service = new ThreadPoolExecutor(poolSize, poolSize, 10, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(queueSize), new ThreadPoolExecutor.CallerRunsPolicy());
}

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);//from  www . j a v a 2  s .  com
            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;
}