线程池ThreadPoolExecutor

  |   0浏览

1-ThreadPool线程池

1.1 为什么要用线程池

线程池ThreadPoolExecutor

1.2 线程池的架构--ThreadPoolExecutor

线程池ThreadPoolExecutor

1.3 线程池的构造方法

线程池ThreadPoolExecutor

package thread20200415;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * 线程池:ThreadPoolExecutor * @author zhaomin * @date 2020/4/15 23:39 */public class ThreadPoolTest {    public static void main(String[] args) {        //一池多线程        //ExecutorService threadPool= Executors.newFixedThreadPool(5);        //一池一线程       // ExecutorService threadPool= Executors.newSingleThreadExecutor();        //一池N线程        ExecutorService threadPool= Executors.newCachedThreadPool();        for(int i=0;i<10;i++) {            threadPool.execute(()->{                System.out.println(Thread.currentThread().getName()+'办理业务');            });            try {                TimeUnit.SECONDS.sleep(1);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        threadPool.shutdown();    }}

1.4线程池中的几个重要参数

线程池ThreadPoolExecutor

1.5线程池底层工作原理

线程池ThreadPoolExecutor线程池ThreadPoolExecutor

9.6 ***的、单一的、可扩容的用哪一种?

一个都不用

线程池ThreadPoolExecutor

1.7 拒绝策略

线程池ThreadPoolExecutor

package thread20200415;import java.util.concurrent.*;/** * 线程池:ThreadPoolExecutor * @author zhaomin * @date 2020/4/15 23:39 */public class ThreadPoolTest {    public static void main(String[] args) {        //一池多线程        //ExecutorService threadPool= Executors.newFixedThreadPool(5);        //一池一线程       // ExecutorService threadPool= Executors.newSingleThreadExecutor();        //一池N线程//        ExecutorService threadPool= Executors.newCachedThreadPool();        /*在实际开发中,要用自定义的线程池        * ThreadPoolExecutor.AbortPolicy()---会报异常        * ThreadPoolExecutor.CallerRunsPolicy()--多余的任务返回给调用者线程来执行        * ThreadPoolExecutor.DiscardPolicy()--直接拒绝,默默工作--最好了        * ThreadPoolExecutor.DiscardOldestPolicy()--踢掉最早来到阻塞队列等待的任务,给新任务腾地方*//**public ThreadPoolExecutor(int corePoolSize,                            int maximumPoolSize,                            long keepAliveTime,                            TimeUnit unit,                             BlockingQueue<Runnable> workQueue,                            ThreadFactory threadFactory,                             RejectedExecutionHandler handler) {*/        ExecutorService threadPool= new ThreadPoolExecutor(                2,                3,                2L,                TimeUnit.SECONDS,                new LinkedBlockingQueue(3),                Executors.defaultThreadFactory(),                new ThreadPoolExecutor.DiscardOldestPolicy());        try {            for (int i = 0; i < 10; i++) {                threadPool.execute(() -> {                    System.out.println(Thread.currentThread().getName() + '办理业务');                });            }        } catch (Exception e) {            e.printStackTrace();        }finally{            threadPool.shutdown();        }    }}

原文地址:https://blog.51cto.com/14234228/2506980