来源丨gongxiongzhuang
hacpai.com/article/1557481151073
在项目里面为了进步性能往往会在主线程里面开启一个新线程去施行,那种做法最便利快速,但是当用户量数据上涨,很显然每次去开启新的线程办事器往往会吃不用,那时就需要线程池来办理和监控线程的形态。
创建多线程的三种姿势java 多线程很常见,若何利用多线程,若何创建线程,java 中有三种体例:
通过继承Thread接口
public class Mytheard1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("thread#1===" + i);
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
通过实现Runnable接口
public class Mytheard2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("thread#2===" + i);
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
通过实现Callable接口
public class Mytheard3 implements Callable<<>Integer> {
@Override
public Integer call() throws Exception{
int sum = 0;
for (int i = 0; i < 30; i++) {
System.out.println("thread#3===" + i);
sum += i;
}
return sum;
}
}
启动上面三个线程
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
// 通过主线程启动本身的线程
// 通过继承 thread 类
Mytheard1 thread1 = new Mytheard1();
thread1.start();
// 通过实现 runnable 接口
Thread thread2 = new Thread(new Mytheard2());
thread2.start();
// 通过实现 callable 接口
Mytheard3 th = new Mytheard3();
FutureTask<<>Integer> result = new FutureTask<>(th);
new Thread(result).start();
// 留意那里都不是间接挪用 run() 办法,而是调运线程类 Thread 的 start 办法,在 Thread 办法内部,会调运当地系统办法,最末会主动调运本身线程类的 run 办法
// 让主线程睡眠
Thread.sleep(1000L);
System.out.println("主线程完毕!用时:"
+ (System.currentTimeMillis() - startTime));
}
上面三种体例更保举通过实现 Runnable接口和实现 Callable接口,因为面向接口编程拓展性更好,并且能够避免 java 单继承的限造。
线程类型的简单申明java 中线程一共有两品种型:守护线程( daemon thread)和用户线程( user thread),又叫非守护线程
守护线程
能够通过 thread.setDaemon(true) 办法设置线程能否为守护线程, thread.setDaemon(true) 必需在 thread.start()之前设置,不然会抛出一个 IllegalThreadStateException 异常。在守护线程中开启的新线程也将是守护线程。守护线程望文生义是用来守护的,是给所有得非守护历程供给办事的,所以在 jvm 施行完所有的非守护历程之后, jvm 就会停行,守护线程也不会再运行,最典型的守护线程就是 java 的垃圾收受接管机造 ( GC)。
非守护线程
java 线程默认设置长短守护线程 thread.setDaemon(false)。当主线程运行完之后,只要主线程里面有非守护线程 jvm 就不会退出,曲到所有的非守护线程施行完之后 jvm 才会退出。
总结:若是把一个线程设置成守护线程,则 jvm 的退出就不会关心当前线程的施行形态。
线程池的利用上面代码中能够间接新起线程,若是 100 个并发同时拜候主线程也就是短时间就启动了 200 个线程,200 个线程同时工做,逻辑上是没有任何问题的,但是如许做对系统资本的开销很大。基于如许的考虑,就要考虑启用线程池,线程池里有良多可用线程资本,若是需要就间接从线程池里拿就是。当不消的时候,线程池会主动帮我们办理。
所以利用线程池次要有以下两个益处:
削减在创建和销毁线程上所花的时间以及系统资本的开销
如不利用线程池,有可能形成系统创建大量线程而招致消耗完系统内存 。
自定义线程池
定义单例线程池:
public class MyPool {
private static MyPool myPool = null;
// 单例线程池中有两种详细的线程池
private ThreadPoolExecutor threadPool = null;
private ScheduledThreadPoolExecutor scheduledPool = null;
public ThreadPoolExecutor getThreadPool() {
return threadPool;
}
public ScheduledThreadPoolExecutor getScheduledPool() {
return scheduledPool;
}
// 设置线程池的各个参数的大小
private int corePoolSize = 10;// 池中所保留的线程数,包罗空闲线程。
private int maximumPoolSize = 20;// 池中允许的更大线程数。
private long keepAliveTime = 3;// 当线程数大于核心时,此为末行前多余的空闲线程期待新使命的最长时间。
private int scheduledPoolSize = 10;
private static synchronized void create() {
if (myPool == null)
myPool = new MyPool();
}
public static MyPool getInstance() {
if (myPool == null)
create();
return myPool;
}
private MyPool() {
// 实例化线程池,那里利用的 LinkedBlockingQueue 做为 workQueue ,利用 DiscardOldestPolicy 做为 handler
this.threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new ThreadPoolExecutor.CallerRunsPolicy());// 不在新线程中施行使命,而是由挪用者所在的线程来施行
// 实例化方案使命线程池
this.scheduledPool = new ScheduledThreadPoolExecutor(scheduledPoolSize);
}
}
创建线程池的次要参数申明:
corePoolSize(int):线程池中连结的线程数量,包罗空闲线程在内。也就是线程池释放的最小线程数量边界。
maximumPoolSize(int): 线程池中嫩包容更大线程数量。
keepAliveTime(long): 空闲线程连结在线程池中的时间,当线程池中线程数量大于 corePoolSize 的时候。
unit(TimeUnit列举类): 上面参数时间的单元,能够是分钟,秒,毫秒等等。
workQueue(BlockingQueue): 使命队列,当线程使命提交到线程池以后,起首放入队列中,然后线程池根据该使命队列依次施行响应的使命。能够利用的 workQueue 有良多,好比:LinkedBlockingQueue 等等。
threadFactory(ThreadFactory类): 新线程产生工场类。
handler(RejectedExecutionHandler类): 当提交线程回绝施行、异常的时候,处置异常的类。该类取值如下:(留意都是内部类)
ThreadPoolExecutor.AbortPolicy: 丢弃使命并抛出RejectedExecutionException 异常。
ThreadPoolExecutor.DiscardPolicy:也是丢弃使命,但是不抛出异常。
ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的使命,然后从头测验考试施行使命,反复此过程。
ThreadPoolExecutor.CallerRunsPolicy:由挪用线程处置该使命。
获取线程池并添加使命:
public void testThreadPool() {
ThreadPoolExecutor pool1 = (ThreadPoolExecutor) Executors.newCachedThreadPool();
pool1.execute(() -> System.out.println("快速线程池中的线程!"));
ThreadPoolExecutor pool2 = MyPool.getInstance().getThreadPool();
pool2.execute(() -> {
System.out.println("pool2 通俗线程池中的线程!");
try {
Thread.sleep(30*1000);} catch (InterruptedException e) {e.printStackTrace();
}
});
System.out.println("pool2 poolSize:"+pool2.getPoolSize());
System.out.println("pool2 corePoolSize:"+pool2.getCorePoolSize());
System.out.println("pool2 largestPoolSize:"+pool2.getLargestPoolSize());
System.out.println("pool2 maximumPoolSize:"+pool2.getMaximumPoolSize());
ScheduledThreadPoolExecutor pool3 = MyPool.getInstance().getScheduledPool();
pool3.scheduleAtFixedRate(() -> System.out.println("方案使命线程池中的线程!"), 0, 5000, TimeUnit.MILLISECONDS);
}
JDK 供给的常用线程池
java 供给了几种常用的线程池,能够快速的供法式员利用
newFixedThreadPool 创建固定大小数量线程池,数量通过传入的参数决定。
newSingleThreadExecutor 创建一个线程容量的线程池,所有的线程依次施行,相当于创建固定命量为 1 的线程池。
newCachedThreadPool 创建可缓存的线程池,没有更大线程限造(现实上是 Integer.MAX_VALUE)。若是用空闲线程期待时间超越一分钟,就封闭该线程。
newScheduledThreadPool 创建方案 (延迟) 使命线程池, 线程池中的线程能够让其在特定的延迟时间之后施行,也能够以固定的时间反复施行(周期性施行)。相当于以前的 Timer 类的利用。
newSingleThreadScheduledExecutor 创建单线程池延迟使命,创建一个线程容量的方案使命。
Spring Boot中利用线程池
若是利用 spring 框架的伴侣,能够间接利用 spring 封拆的线程池,由 spring 容器办理。 Spring Boot中有两种体例设置装备摆设线程池,一种是 自定义设置装备摆设,二种是 修改原生 spring 异步线程池的拆配。
1. 自定义线程池
@Configuration
@EnableAsync// 开启线程池
public class TaskExecutePool {
@Autowired
private TaskThreadPoolConfig config;
@Bean
public Executor myTaskAsyncPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程池大小
executor.setCorePoolSize(config.getCorePoolSize());
// 更大线程数
executor.setMaxPoolSize(config.getMaxPoolSize());
// 队列容量
executor.setQueueCapacity(config.getQueueCapacity());
// 活泼时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
// 线程名字前缀
executor.setThreadNamePrefix("MyExecutor-");
// setRejectedExecutionHandler:当 pool 已经到达 max size 的时候,若何处置新使命
// CallerRunsPolicy:不在新线程中施行使命,而是由挪用者所在的线程来施行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
2. 修改原生 spring 异步线程池的拆配
@Configuration
@EnableAsync
public class NativeAsyncTaskExecutePool implements AsyncConfigurer {
private Logger logger = LoggerFactory.getLogger(this.getClass());
// 注入设置装备摆设类
@Autowired
TaskThreadPoolConfig config;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程池大小
executor.setCorePoolSize(config.getCorePoolSize());
// 更大线程数
executor.setMaxPoolSize(config.getMaxPoolSize());
// 队列容量
executor.setQueueCapacity(config.getQueueCapacity());
// 活泼时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
// 线程名字前缀
executor.setThreadNamePrefix("MyExecutor2-");
// setRejectedExecutionHandler:当 pool 已经到达 max size 的时候,若何处置新使命
// CallerRunsPolicy:不在新线程中施行使命,而是由挪用者所在的线程来施行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, objects) -> {
logger.error("=========================="+ex.getMessage()+"=======================", ex);
logger.error("exception method:"+method.getName());
};
}
}
3. 线程池设置装备摆设类
@Component
public class TaskThreadPoolConfig {
@Value("${task.pool.corePoolSize}")
private int corePoolSize;
@Value("${task.pool.maxPoolSize}")
private int maxPoolSize;
@Value("${task.pool.keepAliveSeconds}")
private int keepAliveSeconds;
@Value("${task.pool.queueCapacity}")
private int queueCapacity;
......// 省略 get(),set() 办法
}
4. 设置装备摆设文件设置装备摆设线程池大小
# spring 线程池task: pool: #核心线程池 corePoolSize: 500 #更大线程池 maxPoolSize: 1000 #活泼时间 keepAliveSeconds: 300 #队列容量 queueCapacity: 505. 需要异步线程施行的使命
@Component
public class AsyncTask {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Async("myTaskAsyncPool") //myTaskAsynPool 即设置装备摆设线程池的办法名,此处若是不写自定义线程池的办法名,会利用默认的线程池
public void doTask1(int i) {
logger.info("Task"+i+"started.");
}
@Async// 利用默认的线程池
public void doTask2(int i) {
if (i == 0) {
throw new NullPointerException();
}
logger.info("Task2-Native"+i+"started.");
}
@Async// 利用默认的线程池并返回参数
public ListenableFuture<<>String> doTask3(int i) {
logger.info("Task3- 返回值"+i+"started.");
return new AsyncResult<>(i + "");
}
}
6. 获取线程池,并施行使命
@Test
public void AsyncTaskTest() {
for (int i = 0; i < 10000; i++) {
try {
// 自定义线程池
asyncTask.doTask1(i);
//spring 异步线程池
asyncTask.doTask2(i);
String text = asyncTask.doTask3(i).get();// 阻塞挪用
System.out.println(text);
String context = asyncTask.doTask3(i).get(1, TimeUnit.SECONDS);// 限时挪用
System.out.println(context);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}
logger.info("All tasks finished.");
}







还没有评论,来说两句吧...