線程池的使用

爲什麼要用線程池?

1.創建/銷燬線程伴隨着系統開銷,使用多線程過於頻繁的創建/銷燬線程,會很大程度上影響處理效率;這裏線程池可以複用線程,線程池可以避免性能降低。
2.線程併發數量過多,搶佔系統資源從而導致阻塞;這裏線程池可以顯示最大線程數量。
3.對線程進行一些簡單的管理

各種線程池的創建方式:

1.創建緩存線程池:

        //創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閒線程,若無可回收,則新建線程。
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

內部實現方式:

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

2.創建單線程池:

 //創建一個線程的線程池,保證任務執行順序和添加任務順序一致
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

內部實現方式:

 public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

3.創建定長線程池

//創建定長線程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

內部實現方式:

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);
    }

4.創建週期線程池

 //創建一個定長線程池,定時及週期性任務執行
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);

內部實現方式:

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
使用線程池執行任務:
        threadPool.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
使用線程池關閉任務:

原理是遍歷線程池中的工作線程,然後逐個調用線程的interrupt方法來中斷線程

        cachedThreadPool.shutdown();

        cachedThreadPool.shutdownNow();

驗證各種線程池特性:

創建執行的任務:任務就是打印各自的"類路徑+@+hashcode"。

    public static class MyRunable implements Runnable {

        public MyRunable(){

        }

        @Override
        public void run() {
            System.out.println("開始處理任務");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("當前執行任務線程" + this.toString());
        }
    }

1.循環創建任務放入CachedThreadPool中執行:

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        for(int i=0;i<10;i++){
            cachedThreadPool.execute(new MyRunable());
        }

打印結果:給多少個任務,就創建多少個線程,緩存線程池不限制線程大小

開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@4ee88e47
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@51dbc572
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@6350da9f
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@34258bc7
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@2dd943ad
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@786a52e0
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@b89fbe7
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@834fe66
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@52922937
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@3d339f20

2.循環創建任務放入SingleThreadPool中執行:

        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();

        for(int i=0;i<10;i++){
            singleThreadPool.execute(new MyRunable());
        }

當前只有一個線程處理任務,任務按照加入順序依序執行:

開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@35b893c9
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@3b7f6b0e
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@3f40f8b9
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@7a426135
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@b95836f
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@4bf2be08
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@73cf02ff
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@1a6d9e7a
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@3d44e8a3
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@639803fd

3.循環創建任務放入FixedThreadPool中執行:
定長線程池,設置最大線程數爲3;

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for(int i=0;i<10;i++){
            fixedThreadPool.execute(new MyRunable());
        }

最多能有3個線程同時處理任務:

開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@f1c2027
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@464fea81
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@72e3cfe6
開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@48881c38
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@7c242fca
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@1dbcfb4
開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@4cb781f1
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@5e44b57
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@781ba496
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@9b51603

4.循環創建任務放入ScheduledThreadPool中執行:

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
        for(int i=0;i<10;i++){
            scheduledExecutorService.schedule(new MyRunable(),4, TimeUnit.SECONDS);
        }

也是需要固定線程大小,不過比fixed多了延遲執行。

開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@2fec69f9
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@1188fc84
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@7b1e0a3d
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@1f4f00c5
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@75d9fb2e
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@2bd3e8dc
開始處理任務
開始處理任務
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@71e6614d
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@1b7097f4
開始處理任務
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@6ed163c2
當前執行任務線程com.example.apple.threadpooluse.MainActivity$MyRunable@34e4f115
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章