线程池

2017-11-21  本文已影响0人  woochen123

定义

Java通过Executors提供四种线程池,分别为:

使用

    static ThreadPoolExecutor threadPoolExecutor;
    private static final BlockingDeque<Runnable> sPoolWorkQueue =
            new LinkedBlockingDeque<>(4);

    //Queue 的参数
    //BlockingQueue:先进先出的一个附列FIFO
    //SynchronousQueue:线程安全的队列,它里面是没有固定的缓存的(Okhttp使用的)
    //PriorityBlockingQueue:无序的可以根据优先级进行排序,指定的对象要实现Comparable作比较

    static {
        threadPoolExecutor = new ThreadPoolExecutor(
                4,//核心线程数,就是线程池里面的核心线程数量
                10,//最大线程数,线程池中的最大线程数
                60,//线程存活的时间,没事干的时候空闲的存活时间,超过这个时间线程就会被销毁
                TimeUnit.SECONDS,//线程存活时间的单位
                sPoolWorkQueue//线程队列
        );
    }


    public void test(){
        threadPoolExecutor.execute(runnnable);
    }
上一篇 下一篇

猜你喜欢

热点阅读