线程池的流程图及策略
Java-五种线程池,四种拒绝策略,三种阻塞队列(常用)
ExecutorService threadPool = Executors.newFixedThreadPool(5);
1.public static ExecutorService newFixedThreadPool()
2.public static ExecutorService newScheduledThreadPool()
3.public static ExecutorService newSingleThreadExecutor()
4.public static ExecutorService newCachedThreadPool()
5. threadPool = new ThreadPoolExecutor();//默认线程池,可控制参数比较多
4种拒绝策略
- AbortPolicy(默认):丢弃任务并抛出RejectedExecutionException异常。
- CallerRunsPolicy:由调用线程处理该任务。
- DiscardPolicy:丢弃任务,但是不抛出异常。可以配合这种模式进行自定义的处理方式。
- DiscardOldestPolicy:丢弃队列最早的未处理任务,然后重新尝试执行任务。
三种阻塞队列:
BlockingQueue<Runnable> workQueue = null;
workQueue = new ArrayBlockingQueue<>(5);//基于数组的先进先出队列,有界
workQueue = new LinkedBlockingQueue<>();//基于链表的先进先出队列,无界
workQueue = new SynchronousQueue<>();//无缓冲的等待队列,无界
流程图
from 蜡笔没了芯