线程池ThreadPool

妖精的绣舞 提交于 2020-03-16 08:28:25

线程池要在执行execute时才会正式创建线程

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)线程池7大参数
corePoolSize:线程池中心的常驻核心线程数
maximumPoolSize:线程池中能够容纳同时执行的最大线程数,此值必须要大于等于!
keepAliveTime:多余的空间线程的存活时间,当前池中线程数量超过corePoolSize时,当空闲时间达到keepAliveTime,多余线程会被销毁直到只剩下corePoolSize个线程为止
TimeUnit:KeepAliveTime的单位 
workQueue:任务队列,被提交但尚未被执行的任务
threadFactory:表示生成线程池中工作线程的线程工厂,用于创建线程,一般默认即可
handler:拒绝策略,表示当前队列满了,并且工作线程大于等于线程池的最大线程数时如何拒绝请求执行的runnable的策略


public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {    return new ThreadPoolExecutor(nThreads, nThreads,                                  0L, TimeUnit.MILLISECONDS,                                  new LinkedBlockingQueue<Runnable>(),                                  threadFactory);}
 
public ThreadPoolExecutor(int corePoolSize,                          int maximumPoolSize,                          long keepAliveTime,                          TimeUnit unit,                          BlockingQueue<Runnable> workQueue,                          ThreadFactory threadFactory) {    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,         threadFactory, defaultHandler);
 在实际工作中
newFixedThreadPool(固定)
newSingleThreadExecutor(单一)
newCachedThreadPool(可变)
的这三个接口都不用,原因在于(1)FixedThreadPool 和 SingleThreadPool 允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM
(2)CachedThreadPool 和 ScheduledThreadPool 允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的线程,从而导致OOM


例子public class MyThreadPoolDemo {    public static void main(String[] args) {        ExecutorService threadPool = new ThreadPoolExecutor(                2,                5,                3L,                TimeUnit.SECONDS,                new ArrayBlockingQueue<Runnable>(3),                Executors.defaultThreadFactory(),                new ThreadPoolExecutor.DiscardPolicy()                //new ThreadPoolExecutor.DiscardOldestPolicy()                //new ThreadPoolExecutor.CallerRunsPolicy()                //new ThreadPoolExecutor.AbortPolicy()                );        try {            for (int i = 1; i <=10 ; i++) {                threadPool.execute(()->{                    System.out.println(Thread.currentThread().getName()                            +"\t 号业务员办理业务");                });            }        } catch (Exception e) {            e.printStackTrace();        } finally {            threadPool.shutdown();        }    }    private static void threadPool() {        ExecutorService threadPool = Executors.newFixedThreadPool(3);//银行网点3个窗口        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();//银行网点1个窗口        ExecutorService threadPool3 = Executors.newCachedThreadPool();//银行网点可扩展窗口        try {            for (int i = 1; i <=30 ; i++) {                threadPool3.execute(()->{                    System.out.println(Thread.currentThread().getName()                            +"\t 号业务员办理业务");                });            }        } catch (Exception e) {            e.printStackTrace();        } finally {            threadPool3.shutdown();        }    }}


 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!