Java ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName)

Here you can find the source of createThreadPoolExecutor(final int queueSize, final String threadName)

Description

create Thread Pool Executor

License

Apache License

Declaration

public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName) {
        ThreadFactory threadFactory = new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, threadName);
                t.setDaemon(true);//from w w w  .j  a v a2  s  .  c o  m
                return t;
            }
        };

        int processors = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(queueSize);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(processors, processors, 2, TimeUnit.SECONDS, queue,
                threadFactory, new ThreadPoolExecutor.DiscardPolicy());
        executor.allowCoreThreadTimeOut(true);
        return executor;
    }
}

Related

  1. createExecutor()
  2. createExecutor(final String name, int count, int keepAlive, final boolean isDaemon)
  3. createQueue(final int queueCapacity)
  4. createScheduledThreadPoolExecutor(final int poolSz, final String threadName)
  5. executeInBackground(Runnable r)
  6. executeInDaemon(Runnable... run)
  7. getBlockingWorkExecutor()
  8. getBoundedThreadPoolExecutor(int maxPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory tFactory)