/**
* Created by 尼恩@疯狂创客圈
*/
package com.example.util;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
public class ThreadUtil
{
/**
* CPU核数
**/
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
/**
* 定制的线程工厂
*/
private static class CustomThreadFactory implements ThreadFactory
{
//线程池数量
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
//线程数量
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String threadTag;
CustomThreadFactory(String threadTag)
{
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
this.threadTag = "apppool-" + poolNumber.getAndIncrement() + "-" + threadTag + "-";
}
@Override
public Thread newThread(Runnable target)
{
Thread t = new Thread(group, target,
threadTag + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
{
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY)
{
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
/**
* 空闲保活时限,单位秒
*/
private static final int KEEP_ALIVE_SECONDS = 30;
/**
* 有界队列size
*/
private static final int QUEUE_SIZE = 10000;
/**
* 核心线程数
*/
private static final int CORE_POOL_SIZE = 0;
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT;
//懒汉式单例创建线程池:用于CPU密集型任务
private static class CpuIntenseTargetThreadPoolLazyHolder
{
//线程池: 用于CPU密集型任务
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
MAXIMUM_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
new LinkedBlockingQueue(QUEUE_SIZE),
new CustomThreadFactory("cpu"));
static
{
EXECUTOR.allowCoreThreadTimeOut(true);
//JVM关闭时的钩子函数
Runtime.getRuntime().addShutdownHook(
new ShutdownHookThread("CPU密集型任务线程池", new Callable<Void>()
{
@Override
public Void call() throws Exception
{
//优雅关闭线程池
shutdownThreadPoolGracefully(EXECUTOR);
return null;
}
}));
}
}
/**
* IO线程池最大线程数
*/
private static final int IO_MAX = Math.max(2, CPU_COUNT * 2);
/**
* IO线程池核心线程数
*/
private static final int IO_CORE = 0;
/**
* 获取执行CPU密集型任务的线程池
*
* @return
*/
public static ThreadPoolExecutor getCpuIntenseTargetThreadPool()
{
return CpuIntenseTargetThreadPoolLazyHolder.EXECUTOR;
}
//懒汉式单例创建线程池:用于IO密集型任务
private static class IoIntenseTargetThreadPoolLazyHolder
{
//线程池: 用于IO密集型任务
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
IO_MAX,
IO_MAX,
KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
new LinkedBlockingQueue(QUEUE_SIZE),
new CustomThreadFactory("io"));
static
{
EXECUTOR.allowCoreThreadTimeOut(true);
//JVM关闭时的钩子函数
Runtime.getRuntime().addShutdownHook(
new ShutdownHookThread("IO密集型任务线程池", new Callable<Void>()
{
@Override
public Void call() throws Exception
{
//优雅关闭线程池
shutdownThreadPoolGracefully(EXECUTOR);
return null;
}
}));
}
}
/**
* 获取执行IO密集型任务的线程池
*
* @return
*/
public static ThreadPoolExecutor getIoIntenseTargetThreadPool()
{
return IoIntenseTargetThreadPoolLazyHolder.EXECUTOR;
}
/**
* 混合线程池
*/
private static final int MIXED_CORE = 0; //混合线程池核心线程数
private static final int MIXED_MAX = 128; //最大线程数
private static final String MIXED_THREAD_AMOUNT = "mixed.thread.amount";
//懒汉式单例创建线程池:用于混合型任务
private static class MixedTargetThreadPoolLazyHolder
{
//首先从环境变量 mixed.thread.amount 中获取预先配置的线程数
//如果没有对 mixed.thread.amount 做配置,则使用常量 MIXED_MAX 作为线程数
private static final int max = (null != System.getProperty(MIXED_THREAD_AMOUNT)) ?
Integer.parseInt(System.getProperty(MIXED_THREAD_AMOUNT)) : MIXED_MAX;
//线程池: 用于混合型任务
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
max,
max,
KEEP_ALIVE_SECONDS,
TimeUnit.SECONDS,
new LinkedBlockingQueue(QUEUE_SIZE),
new CustomThreadFactory("mixed"));
static
{
EXECUTOR.allowCoreThreadTimeOut(true);
//JVM关闭时的钩子函数
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread("混合型任务线程池", new Callable<Void>()
{
@Override
public Void call() throws Exception
{
//优雅关闭线程池
shutdownThreadPoolGracefully(EXECUTOR);
return null;
}
}));
}
}
/**
* 获取执行混合型任务的线程池 *
*
* @return
*/
public static ThreadPoolExecutor getMixedTargetThreadPool()
{
return MixedTargetThreadPoolLazyHolder.EXECUTOR;
}
//懒汉式单例创建线程池:用于定时任务、顺序排队执行任务
static class SeqOrScheduledTargetThreadPoolLazyHolder
{
//线程池:用于定时任务、顺序排队执行任务
static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(
1,
new CustomThreadFactory("seq"));
static
{
//JVM关闭时的钩子函数
Runtime.getRuntime().addShutdownHook(
new ShutdownHookThread("定时和顺序任务线程池", new Callable<Void>()
{
@Override
public Void call() throws Exception
{
//优雅关闭线程池
shutdownThreadPoolGracefully(EXECUTOR);
return null;
}
}));
}
}
/**
* 获取可调度线程池(包含提交延迟、定时、周期性、顺�