public class ThreadPoolUtils
{
// 线程池核心线程数
private static int CORE_POOL_SIZE = 5;
// 线程池最大线程数
private static int MAX_POOL_SIZE = 100;
// 额外线程空状态生存时间
private static int KEEP_ALIVE_TIME = 10000;
/** {@value} */
public static final int DEFAULT_THREAD_POOL_SIZE = 3;
/** {@value} */
public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
/** {@value} */
public static final QueueProcessingType DEFAULT_TASK_PROCESSING_TYPE = QueueProcessingType.FIFO;
// 阻塞队列。当核心线程都被占用,且阻塞队列已满的情况下,才会开启额外线程。
private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(
20);
private Handler mHander = new Handler();
// 线程工厂
private static ThreadFactory threadFactory = new ThreadFactory()
{
private final AtomicInteger integer = new AtomicInteger();
@Override
public Thread newThread(Runnable r)
{
return new Thread(r, "myThreadPool thread:"
+ integer.getAndIncrement());
}
};
// 线程池
private static ExecutorService threadPool;
static
{
/* threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue, threadFactory);*/
threadPool = createExecutor(DEFAULT_THREAD_POOL_SIZE, DEFAULT_THREAD_PRIORITY , DEFAULT_TASK_PROCESSING_TYPE);
}
/**
* 从线程池中抽取线程,执行指定的Runnable对象
*
* @param runnable
*/
public static void execute(Runnable runnable, boolean isAwaitTermination, RequestParam param, RequestListener listener)
{
try
{
threadPool.execute(runnable);
if (isAwaitTermination)
{
threadPool.shutdown();
try
{
threadPool.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e)
{
}
}
} catch (Exception e)
{
}
}
/**
* 从线程池中抽取线程,执行指定的Runnable对象
*
* @param runnable
*/
public static void execute(Runnable runnable, boolean isAwaitTermination)
{
try
{
threadPool.execute(runnable);
if (isAwaitTermination)
{
threadPool.shutdown();
try
{
threadPool.awaitTermination(Long.MAX_VALUE,
TimeUnit.NANOSECONDS);
} catch (InterruptedException e)
{
}
}
} catch (Exception e)
{
}
}
/** Creates default implementation of task executor */
public static ExecutorService createExecutor(int threadPoolSize, int threadPriority, QueueProcessingType tasksProcessingType) {
boolean lifo = tasksProcessingType == QueueProcessingType.LIFO;
BlockingQueue<Runnable> taskQueue = lifo ? new LIFOLinkedBlockingDeque<Runnable>() : new LinkedBlockingQueue<Runnable>();
return new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS, taskQueue, createThreadFactory(threadPriority));
}
/** Creates default implementation of {@linkplain ThreadFactory thread factory} for task executor */
private static ThreadFactory createThreadFactory(int threadPriority) {
return new DefaultThreadFactory(threadPriority);
}
private static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
private final int threadPriority;
DefaultThreadFactory(int threadPriority) {
this.threadPriority = threadPriority;
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) t.setDaemon(false);
t.setPriority(threadPriority);
return t;
}
}
}