线程池
1、分类
1)、固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
2)、一个线程池
ExecutorService executorService = Executors.newSingleThreadExecutor();
3)、根据需要创建线程的线程池,没有核心线程池。每次
ExecutorService executorService = Executors.newCachedThreadPool();
这三个都是用 ThreadPoolExecutor 实现的。创建ThreadPoolExecutor 主要参数有:
int corePoolSize 核心线程池大小,这个线程是一直存活的,不会销毁
int maximumPoolSize 最大线程池大小,如果核心线程用完了,再有任务时会创建一个线程,所有的线程总数不能大于这个数。
long keepAliveTime 大于核心线程数线程,空闲的存活时间
TimeUnit unit keepAliveTime 时间单位
BlockingQueue<Runnable> workQueue 存放线程的队列,当没有多余的线程来执行线程时存放线程
ThreadFactory threadFactory 线程工厂,用来创建工作线程
RejectedExecutionHandler handler 拒绝策略,当线程队列存放满时再提交任务线程时处理方法。
具体实现,看源码。主要关注 corePoolSize,maximumPoolSize,BlockingQueue 参数就可以了。
4)、CachedThreadPool是一个会根据需要创建新线程的线程池。
ExecutorService executor = Executors.newCachedThreadPool();
CachedThreadPool使用没有容量的SynchronousQueue作为线程池的工作队列,但CachedThreadPool的maximumPool是无界的。这意味着,如果主线程提交任务的速度高于maximumPool中线程处理任务的速度时,CachedThreadPool会不断创建新线程。极端情况下,CachedThreadPool会因为创建过多线程而耗尽CPU和内存资源。
4)、调度线程池 ScheduledThreadPoolExecutor 是ThreadPoolExecutor 的子类
主要用来在给定的延迟之后运行任务,或者定期执行任务。ScheduledThreadPoolExecutor的功能与Timer类似,但ScheduledThreadPoolExecutor功能更强大、更灵活。Timer对应的是单个后台线程,而ScheduledThreadPoolExecutor可以在构造函数中指定多个对应的后台线程数。
定义:
class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService
创建:
ScheduledThreadPoolExecutor ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
使用的队列是:DelayedWorkQueue,就是使用DelayQueue封装了一个PriorityQueue,这个PriorityQueue会对队列中的ScheduledFutureTask进行排序。排序时,time小的排在前面(时间早的任务将被先执行)。如果两个ScheduledFutureTask的time相同,就比较sequenceNumber,sequenceNumber小的排在前面(也就是说,如果两个任务的执行时间相同,那么先提交的任务将被先执行)。
2、阻塞队列
ArrayBlockingQueue 数组构成的有界阻塞队列
LinkedBlockingQueue 链表组成的有界阻塞队列
PriorityBlockingQueue 支持优先级排序的无界阻塞队列
DelayQueue 使用优先级队列实现的无界阻塞队列
SynchronousQueue 不存储元素的阻塞队列
LinkedTransferQueue 链表组成的无界阻塞队列
LinkedBlockingDeque 链表组成的双向阻塞队列
3、拒绝策略
AbortPolicy 直接抛出异常,默认策略。
CallerRunsPolicy 使用调用都的线程来运行任务线程
DiscardOldestPolicy 丢弃队列中最老的没有执行的一个任务,然后执行executor 执行提交的任务
DiscardPolicy 直接丢弃提交的任务
1、分类
1)、固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
2)、一个线程池
ExecutorService executorService = Executors.newSingleThreadExecutor();
3)、根据需要创建线程的线程池,没有核心线程池。每次
ExecutorService executorService = Executors.newCachedThreadPool();
这三个都是用 ThreadPoolExecutor 实现的。创建ThreadPoolExecutor 主要参数有:
int corePoolSize 核心线程池大小,这个线程是一直存活的,不会销毁
int maximumPoolSize 最大线程池大小,如果核心线程用完了,再有任务时会创建一个线程,所有的线程总数不能大于这个数。
long keepAliveTime 大于核心线程数线程,空闲的存活时间
TimeUnit unit keepAliveTime 时间单位
BlockingQueue<Runnable> workQueue 存放线程的队列,当没有多余的线程来执行线程时存放线程
ThreadFactory threadFactory 线程工厂,用来创建工作线程
RejectedExecutionHandler handler 拒绝策略,当线程队列存放满时再提交任务线程时处理方法。
具体实现,看源码。主要关注 corePoolSize,maximumPoolSize,BlockingQueue 参数就可以了。
4)、CachedThreadPool是一个会根据需要创建新线程的线程池。
ExecutorService executor = Executors.newCachedThreadPool();
CachedThreadPool使用没有容量的SynchronousQueue作为线程池的工作队列,但CachedThreadPool的maximumPool是无界的。这意味着,如果主线程提交任务的速度高于maximumPool中线程处理任务的速度时,CachedThreadPool会不断创建新线程。极端情况下,CachedThreadPool会因为创建过多线程而耗尽CPU和内存资源。
4)、调度线程池 ScheduledThreadPoolExecutor 是ThreadPoolExecutor 的子类
主要用来在给定的延迟之后运行任务,或者定期执行任务。ScheduledThreadPoolExecutor的功能与Timer类似,但ScheduledThreadPoolExecutor功能更强大、更灵活。Timer对应的是单个后台线程,而ScheduledThreadPoolExecutor可以在构造函数中指定多个对应的后台线程数。
定义:
class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService
创建:
ScheduledThreadPoolExecutor ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
使用的队列是:DelayedWorkQueue,就是使用DelayQueue封装了一个PriorityQueue,这个PriorityQueue会对队列中的ScheduledFutureTask进行排序。排序时,time小的排在前面(时间早的任务将被先执行)。如果两个ScheduledFutureTask的time相同,就比较sequenceNumber,sequenceNumber小的排在前面(也就是说,如果两个任务的执行时间相同,那么先提交的任务将被先执行)。
2、阻塞队列
ArrayBlockingQueue 数组构成的有界阻塞队列
LinkedBlockingQueue 链表组成的有界阻塞队列
PriorityBlockingQueue 支持优先级排序的无界阻塞队列
DelayQueue 使用优先级队列实现的无界阻塞队列
SynchronousQueue 不存储元素的阻塞队列
LinkedTransferQueue 链表组成的无界阻塞队列
LinkedBlockingDeque 链表组成的双向阻塞队列
3、拒绝策略
AbortPolicy 直接抛出异常,默认策略。
CallerRunsPolicy 使用调用都的线程来运行任务线程
DiscardOldestPolicy 丢弃队列中最老的没有执行的一个任务,然后执行executor 执行提交的任务
DiscardPolicy 直接丢弃提交的任务
这几个拒绝策略都是 ThreadPoolExecutor 内部类,源码很简单。