【JUC】006-线程池
#
目录
一、概述
1、池化技术
2、线程池的好处
3、线程池知识核心
二、3个方法
1、Executors.newSingleThreadExecutor()
概述:
代码实现:
运行结果:
PS:
2、Executors.newFixedThreadPool(int num)
概述:
代码实现:
运行结果:
PS:
3、Executors.newCachedThreadPool()
概述:
代码实现:
运行结果:
PS:
三、7个参数
1、7个参数引出
构造函数及7个参数的解释:
阿里巴巴Java编程规约:
图解:
2、手动创建线程池
代码实现:
运行结果:
四、4种拒绝策略
1、不处理,抛出异常
2、哪来的回到哪里去
3、不处理,也不抛出异常
4、尝试获取最早被使用的线程资源
五、最大线程到底该如何设置
1、CPU密集型和IO密集型
CPU密集型:
IO密集型:
一、概述
1、池化技术
程序的运行需要占用系统资源,为了减少系统消耗、提升系统性能并方便管理,就有了池化技术;
2、线程池的好处
线程复用、可以控制最大并发数、管理线程;
3、线程池知识核心
3个方法、7个参数、4种拒绝策略;
二、3个方法
1、Executors.newSingleThreadExecutor()
概述:
创建单个线程的线程池;
代码实现:
package com.zibo.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//Executors工具类:三个方法
//使用线程池创建线程
public class Demo01 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建单个线程的线程池
//Executors.newFixedThreadPool(5);//创建固定线程数量的线程池
//Executors.newCachedThreadPool();//创建不固定线程数量的线程池,大小自动伸缩
try {
for (int i = 0; i < 10; i++) {
//创建线程并执行
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + "==>ok!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完需要关闭,释放内存
threadPool.shutdown();
}
}
}
运行结果:
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
pool-1-thread-1==>ok!
PS:
因为线程池只有一个线程,所以线程名字都一样;
2、Executors.newFixedThreadPool(int num)
概述:
创建固定线程数量的线程池;
代码实现:
package com.zibo.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//Executors工具类:三个方法
//使用线程池创建线程
public class Demo01 {
public static void main(String[] args) {
//ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建单个线程的线程池
ExecutorService threadPool = Executors.newFixedThreadPool(5);//创建固定线程数量的线程池
//Executors.newCachedThreadPool();//创建不固定线程数量的线程池,大小自动伸缩
try {
for (int i = 0; i < 10; i++) {
//创建线程并执行
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + "==>ok!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完需要关闭,释放内存
threadPool.shutdown();
}
}
}
运行结果:
pool-1-thread-1==>ok!
pool-1-thread-4==>ok!
pool-1-thread-3==>ok!
pool-1-thread-2==>ok!
pool-1-thread-3==>ok!
pool-1-thread-4==>ok!
pool-1-thread-5==>ok!
pool-1-thread-1==>ok!
pool-1-thread-3==>ok!
pool-1-thread-2==>ok!
PS:
线程池里面有5条线程,所以会出现不同的线程被调用;
3、Executors.newCachedThreadPool()
概述:
创建不固定线程数量的线程池,大小自动伸缩;
代码实现:
package com.zibo.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//Executors工具类:三个方法
//使用线程池创建线程
public class Demo01 {
public static void main(String[] args) {
//ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建单个线程的线程池
//ExecutorService threadPool = Executors.newFixedThreadPool(5);//创建固定线程数量的线程池
ExecutorService threadPool = Executors.newCachedThreadPool();//创建不固定线程数量的线程池,大小自动伸缩
try {
for (int i = 0; i < 10; i++) {
//创建线程并执行
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + "==>ok!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完需要关闭,释放内存
threadPool.shutdown();
}
}
}
运行结果:
pool-1-thread-1==>ok!
pool-1-thread-5==>ok!
pool-1-thread-4==>ok!
pool-1-thread-3==>ok!
pool-1-thread-2==>ok!
pool-1-thread-8==>ok!
pool-1-thread-9==>ok!
pool-1-thread-7==>ok!
pool-1-thread-6==>ok!
pool-1-thread-5==>ok!
PS:
自由伸缩,可大可小;
三、7个参数
1、7个参数引出
通过源码我们发现,上面三个创建线程池的方法都是使用ThreadPoolExecutor类的构造函数创建的,而且ThreadPoolExecutor类的构造函数里面有7个参数;
构造函数及7个参数的解释:
public ThreadPoolExecutor(int corePoolSize, //核心线程池大小
int maximumPoolSize, //最大线程池大小
long keepAliveTime, //保持存活时间(超时了没有被使用就会被释放)
TimeUnit unit, //超时的时间单位
BlockingQueue<Runnable> workQueue, //阻塞队列
ThreadFactory threadFactory, //线程工厂,用于创建线程,一般不变
RejectedExecutionHandler handler) {//拒绝策略
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
阿里巴巴Java编程规约:
图解:
2、手动创建线程池
代码实现:
package com.zibo.pool;
import java.util.concurrent.*;
//Executors工具类:三个方法
//使用线程池创建线程
public class Demo01 {
public static void main(String[] args) {
//自定义线程池,在工作中只会用这种方式
ExecutorService threadPool = new ThreadPoolExecutor(
2,//核心线程数量
5,//最大线程数量
3,//保持存活时间
TimeUnit.SECONDS,//时间单位
new LinkedBlockingDeque<>(3),//阻塞队列
Executors.defaultThreadFactory(),//线程工厂
new ThreadPoolExecutor.AbortPolicy()//所有线程都被占用了,阻塞队列也满了,还有线程进来,就不再处理,并抛出异常
);
try {
//最大承载量 = 最大线程数量 + 阻塞队列数量;
for (int i = 0; i < 10; i++) {//这里最大是8,超出就会抛出异常
//创建线程并执行
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + "==>ok!");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//线程池用完需要关闭,释放内存
threadPool.shutdown();
}
}
}
运行结果:
四、4种拒绝策略
1、不处理,抛出异常
new ThreadPoolExecutor.AbortPolicy():所有线程都被占用了,阻塞队列也满了,还有线程进来,就不再处理,并抛出异常;
2、哪来的回到哪里去
new ThreadPoolExecutor.CallerRunsPolicy()
3、不处理,也不抛出异常
new ThreadPoolExecutor.DiscardPolicy()
4、尝试获取最早被使用的线程资源
new ThreadPoolExecutor.DiscardOldestPolicy()
五、最大线程到底该如何设置
1、CPU密集型和IO密集型
CPU密集型:
cpu是多少核,就是几,可以保持CPU效率最高;
//获取核数:
Runtime.getRuntime().availableProcessors()
IO密集型:
判断程序中耗IO的线程数量,设置大于此数量(一般两倍)即可;
还没有评论,来说两句吧...