1. 为什么使用线程池
主要是为了防止资源的不足,因为频繁的创建和销毁线程是需要大量资源的。尤其是:线程执行时>线程创建时间+线程销毁时间,这个时候是会堆积大量的线程的。线程池其实就是类似于数据库连接池都是为了减少创建和销毁,提高资源的利用率。
2. 使用线程池的风险
1、死锁:任何的多线程都是有可能发生时死锁的情况,就是线程之间在互相等待
2、 资源不足:这个原因主要是线程池太大造成,正常来说合理创建线程池大小一般不会出现这个问题
3、线程泄漏:
4、请求过载
3、常用的几种线程池(java: java.util.concurrent.Executors)
1、newCachedThreadPool:这是第一种可缓存线程池,线程池为无限大,当执行当前任务时上一个任务已经完成,会复用执行上一个任务的线程,而不用每次新建线程
如果上一个线程没有结束则会新建线程
public class CachedThreadPool {
private static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
public static void main(String args[]){
for(int i=0;i<10;i++){
try{
if(i==4){
Thread.sleep(2000);
}
}catch (Exception e){
e.getStackTrace();
}
cachedThreadPool.execute(new Runnable() {
public void run(){
try{
Thread.sleep(1000);
}catch (Exception e){
e.getStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在被执行");
}
});
}
}
}
执行结果:
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-4正在被执行
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-4正在被执行
pool-1-thread-6正在被执行
pool-1-thread-5正在被执行
因为在i=5的时候多停留了2秒所以前面的线程1-4都释放了,所以后面线程就复用了前面的线程,这就是这种线程池的好处
2、newFixedThreadPool:这是个可重用固定个数的线程池,当当前线程数大于总数则会进行等待,等待线程池内的线程执行完,相对来说比较少占内存,如果等待线程过多也是相对消耗资源的
public class FixedThreadPool {
private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
public static void main(String args[]){
for(int i=0;i<10;i++){
fixedThreadPool.execute(new Runnable() {
public void run(){
System.out.println(Thread.currentThread().getName()+"正在被执行");
try{
Thread.sleep(1000);
}catch (Exception e){
e.getStackTrace();
}
}
});
}
}
}
执行结果:
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-4正在被执行
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
pool-1-thread-3正在被执行
pool-1-thread-4正在被执行
pool-1-thread-1正在被执行
pool-1-thread-2正在被执行
这里就很明显可以看出,线程的执行顺序其实就是类似于负载均衡的轮询算法一样,线程池大小是4所以当执行线程数大于4就会进行等待,其他线程执行完后再调用线程池内可用线程。
3、ScheduledThreadPool:这是个可重用固定个数的线程池,当前线程数大于总数则会进行等待,并且可以设置线程延迟执行时间
public class ScheduledThreadPool {
private static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4);
public static void main(String args[]){
for(int i=0;i<10;i++){
System.out.println("线程"+i+"开始执行时间"+new Date());
scheduledThreadPool.schedule(new Runnable() {
public void run(){
System.out.println(Thread.currentThread().getName()+"正在被执行");
System.out.println(new Date());
}
}, 3, TimeUnit.SECONDS);
}
}
}
结果:
线程0开始执行时间Wed May 29 23:05:05 CST 2019
线程1开始执行时间Wed May 29 23:05:05 CST 2019
线程2开始执行时间Wed May 29 23:05:05 CST 2019
线程3开始执行时间Wed May 29 23:05:05 CST 2019
线程4开始执行时间Wed May 29 23:05:05 CST 2019
线程5开始执行时间Wed May 29 23:05:05 CST 2019
线程6开始执行时间Wed May 29 23:05:05 CST 2019
线程7开始执行时间Wed May 29 23:05:05 CST 2019
线程8开始执行时间Wed May 29 23:05:05 CST 2019
线程9开始执行时间Wed May 29 23:05:05 CST 2019
pool-1-thread-1正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-3正在被执行
Wed May 29 23:05:08 CST 2019
pool-1-thread-2正在被执行
Wed May 29 23:05:08 CST 2019
从结果就可以看出,我们所有的线程都是在23:05:05开始启动线程,直到23:05:08才开始执行,和我们设置的3秒后执行完全吻合。
4、newSingleThreadExecutor:单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。缺点就是单线程执行,当线程多的时候执行速度比较慢
public class SingleThreadPool {
private static ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
public static void main(String args[]){
for(int i=0;i<10;i++){
final int index = i;
singleThreadPool.execute(new Runnable() {
public void run(){
try{
System.out.println(Thread.currentThread().getName()+"正在被执行,打印的值是:"+index);
Thread.sleep(1000);
}catch (Exception e){
e.getStackTrace();
}
}
});
}
}
}
结果:
pool-1-thread-1正在被执行,打印的值是:0
pool-1-thread-1正在被执行,打印的值是:1
pool-1-thread-1正在被执行,打印的值是:2
pool-1-thread-1正在被执行,打印的值是:3
pool-1-thread-1正在被执行,打印的值是:4
pool-1-thread-1正在被执行,打印的值是:5
pool-1-thread-1正在被执行,打印的值是:6
pool-1-thread-1正在被执行,打印的值是:7
pool-1-thread-1正在被执行,打印的值是:8
pool-1-thread-1正在被执行,打印的值是:9
我们可以看出所有的线程都是用的线程1来执行的并且是有序的,这就是单线程池
5、自定义线程池(ThreadPoolExecutor和BlockingQueue连用)
BlockingQueue是双缓冲队列。BlockingQueue内部使用两条队列,允许两个线程同时向队列一个存储。,一个取出操作。在保证并发安全的同时,提高了队列的存取效率。
常用的几种BlockingQueue:
ArrayBlockingQueue(int i):规定大小的BlockingQueue,其构造必须指定大小。其所含的对象是FIFO顺序排序的。
LinkedBlockingQueue()或者(int i):大小不固定的BlockingQueue,若其构造时指定大小,生成的BlockingQueue有大小限制,不指定大小,其大小有Integer.MAX_VALUE来决定。其所含的对象是FIFO顺序排序的。
PriorityBlockingQueue()或者(int i):类似于LinkedBlockingQueue,但是其所含对象的排序不是FIFO,而是依据对象的自然顺序或者构造函数的Comparator决定。
SynchronizedQueue():特殊的BlockingQueue,对其的操作必须是放和取交替完成。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
这是我们缓存池的源码,我们可以看出其实就是基于SynchronousQueue和ThreadPoolExecutor实现的。所以我们如果有需要自定义线程池也是可以通过双缓冲队列和ThreadPoolExecutor组合是实现自定义。
----------------------写的不好,仅供参考---------------------------------------