线程池API,了解一下

三种线程池

    public static void main(String[] args) {
//      getCachedThreadPool();
//      getSingleThreadExecutor();
        getFixedThreadPool();
    }
    private static void getFixedThreadPool() {
        //指定线程数 共享无界队列方式来运行这些线程
//      某个线程被显示的关闭之前,池中的线程将一直存在
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3333);
        for (int i =0 ; i <11111 ; i ++)
        {
            final  int no = i ;
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        System.out.println("into" + no);
                        Thread.sleep(111L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            };
            newFixedThreadPool.execute(runnable);
            int activeCount = Thread.currentThread().activeCount();
            System.out.println("size = " + activeCount );

        }
        newFixedThreadPool.shutdown();

    }
    static void getCachedThreadPool() {
        /*
         * 创建一个缓存池大小可根据需要伸缩的线程池
         * 当前存活的线程可以自动伸缩
         */
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        for (int i =0 ; i <1111120 ; i ++)
        {
            final  int no = i ;
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        System.out.println("into" + no);
                        Thread.sleep(111L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            };
            newCachedThreadPool.execute(runnable);
            int activeCount = Thread.currentThread().activeCount();
            System.out.println("size = " + activeCount );

        }
        newCachedThreadPool.shutdown();

    }
    static void getSingleThreadExecutor() {
        // 创建一个单线程的线程池
        ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
        // 创建一个LinkedBlockingQueue的 一个 大小的线程池  

        for (int i = 0; i < 110; i++) {
            final int no = i;
            Runnable runnable = new Runnable() {
                public void run() {
                    try {
                        System.out.println("into" + no);
                        Thread.sleep(111L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            newSingleThreadExecutor.execute(runnable);
            System.out.println(Thread.currentThread().activeCount() + " = size");  
        }

        newSingleThreadExecutor.shutdown();
        System.out.println("thread  main end ");
    }

线程池工作机制及其原理

线程池的核心的两个队列
线程等待池,线程队列BlockingQueue
任务处理池,正在工作的Thread列表(hashset<Worker>)

线程池的核心的参数
核心池的大小:固定大小,设定好之后,线程池的稳定峰值,达到这个值之后池的线程数大小不会释放

最大处理线程池数:当线程池里面的线程数超过核心池的大小,最大处理线程池数会动态创建与回收线程池里面的线程的资源

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/java_sparrow/article/details/81228561