线程池ThreadPoolExecutor

2021-05-05 01:31

阅读:607

标签:new t   线程   min   public   地方   底层工作原理   size   开发   rtp   

1-ThreadPool线程池

1.1 为什么要用线程池

技术图片

1.2 线程池的架构--ThreadPoolExecutor

技术图片

1.3 线程池的构造方法

技术图片

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{
                System.out.println(Thread.currentThread().getName()+"办理业务");
            });
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        threadPool.shutdown();
    }
}

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

技术图片

1.5线程池底层工作原理

技术图片
技术图片

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

一个都不用

技术图片

1.7 拒绝策略

技术图片

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 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  {
                    System.out.println(Thread.currentThread().getName() + "办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            threadPool.shutdown();
        }
    }
}

线程池ThreadPoolExecutor

标签:new t   线程   min   public   地方   底层工作原理   size   开发   rtp   

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


评论


亲,登录后才可以留言!