1.线程池体系结构
Executor 负责线程的使用和调度的 根接口
|--ExecutorService 接口: 线程池的主要接口。增加了返回Future 对象
|--ThreadPoolExecutor 线程池的实现类
|--ScheduledExceutorService 接口: 负责线程的调度
|--ScheduledThreadPoolExecutor : 继承ThreadPoolExecutor,实现了ScheduledExecutorService
2.单例模式定义
简单理解就是一个类只能有一个实例, 线程池使用中只会创建一个实例
3.饿汉式线程池执行工厂
import cn.hutool.core.thread.NamedThreadFactory;
import lombok.NonNull;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 描述:饿汉式 线程池执行器工厂.
* 1.单例
* 2.不管有没有调用者都会提前创建实例,资源利用效率低
* @author wangkai
*/
public final class HungryThreadPoolFactory {
private HungryThreadPoolFactory() {
}
/**
* 全局访问点.
* @param runnable runnable
*/
public static void execute(@NonNull Runnable runnable) {
getInstance().execute(runnable);
}
/**
* 静态公用工厂方法,返回唯一实例.
* @return ThreadPoolExecutor
*/
public static ThreadPoolExecutor getInstance() {
return HungryThreadPoolFactory.THREAD_POOL_EXECUTOR;
}
static final int CPU = Runtime.getRuntime().availableProcessors();
static final int CORE_POOL_SIZE = CPU + 1;
static final int MAXIMUM_POOL_SIZE = CPU * 2 + 1;
static final long KEEP_ALIVE_TIME = 1L;
static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
static final int MAX_QUEUE_NUM = 1024;
/**
* 静态私有成员变量.
*/
private static ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT,
new LinkedBlockingQueue<>(MAX_QUEUE_NUM),
new NamedThreadFactory("ThreadPoolExecutorFactory-", false),
new ThreadPoolExecutor.AbortPolicy());
}
4.懒汉式线程池执行工厂
import cn.hutool.core.thread.NamedThreadFactory;
import lombok.NonNull;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 描述:懒汉式 线程池执行器工厂.
* 1.单例
* 2.延迟加载:解决了饿汉式不管有没有调用都创建实例的弊端。但是多线程访问可能会创建多个实例,所以需要解决线程安全问题
* @author wangkai
*/
public final class LazyThreadPoolFactory {
private LazyThreadPoolFactory() {
}
/**
* 全局访问点.
* @param runnable runnable
*/
public static void execute(@NonNull Runnable runnable) {
getInstance().execute(runnable);
}
/**
* 静态私有成员变量.
*/
private static ThreadPoolExecutor THREAD_POOL_EXECUTOR = null;
public static ThreadPoolExecutor getInstance() {
generateInstance();
return LazyThreadPoolFactory.THREAD_POOL_EXECUTOR;
}
static final int CPU = Runtime.getRuntime().availableProcessors();
static final int CORE_POOL_SIZE = CPU + 1;
static final int MAXIMUM_POOL_SIZE = CPU * 2 + 1;
static final long KEEP_ALIVE_TIME = 1L;
static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
static final int MAX_QUEUE_NUM = 1024;
public static void generateInstance() {
//校验实例是否已生成,避免多线程访问重复生成实例
if (THREAD_POOL_EXECUTOR == null) {
THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT,
new LinkedBlockingQueue<>(MAX_QUEUE_NUM),
new NamedThreadFactory("ThreadPoolExecutorFactory-", false),
new ThreadPoolExecutor.AbortPolicy());
}
}
}
5.静态内部类实现线程池工厂(开发中使用的一中方式)
import cn.hutool.core.thread.NamedThreadFactory;
import lombok.NonNull;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 描述:静态内部类 线程池执行器工厂.
* 1.单例
* 2.饿汉式+延迟加载
* @author wangkai
*/
public final class SafeThreadPoolFactory {
private SafeThreadPoolFactory() {
}
/**
* 全局访问点.
* @param runnable runnable
*/
public static void execute(@NonNull Runnable runnable) {
ThreadPoolExecutorHolder.THREAD_POOL_EXECUTOR.execute(runnable);
}
/**
* 静态内部类创建实例(单例).
* 优点:被调用时才会创建一次实例
*/
private static class ThreadPoolExecutorHolder {
static final int CPU = Runtime.getRuntime().availableProcessors();
static final int CORE_POOL_SIZE = CPU + 1;
static final int MAXIMUM_POOL_SIZE = CPU * 2 + 1;
static final long KEEP_ALIVE_TIME = 1L;
static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
static final int MAX_QUEUE_NUM = 1024;
//static变量只会初始化一次
public static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT,
new LinkedBlockingQueue<>(MAX_QUEUE_NUM),
new NamedThreadFactory("ThreadPoolExecutorFactory-", false),
new ThreadPoolExecutor.AbortPolicy());
}
}