Java多線程4 初識線程池

Java多線程目錄

前言

Java為什么引入線程池?
創建線程示例

    new Thread(new Runnable() {

            @Override
            public void run() {
            
            }
        }).start();
new Thread的弊端
  1. 每次new Thread新建對象性能差。
  2. 線程缺乏統一管理,可能無限制新建線程,相互之間競爭,及可能占用過多系統資源導致死機或oom。
  3. 缺乏更多功能,如定時執行、定期執行、線程中斷。
相比new Thread,Java提供的四種線程池的好處在于:
  1. 重用存在的線程,減少對象創建、消亡的開銷,性能佳。
  2. 可有效控制最大并發線程數,提高系統資源的使用率,同時避免過多資源競爭,避免堵塞。
  3. 提供定時執行、定期執行、單線程、并發數控制等功能。


    image.png

1.Java四種線程池

Java通過Executors提供四種線程池,分別為:
newFixedThreadPool 創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。
newSingleThreadExecutor 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。
newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
newScheduledThreadPool 創建一個定長線程池,支持定時及周期性任務執行。

1.1 newFixedThreadPool

創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。

public class ThreadPoolTest {
    public static void main(String[] args) {

        //固定線程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        for (int i = 1; i < 10; i++) {
            int finalI = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j < 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI

                        );

                    }
                }
            });
        }
    }
}
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-2 is looping of 1 for task of 5
pool-1-thread-3 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-2 is looping of 2 for task of 5
pool-1-thread-3 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-2 is looping of 3 for task of 5
pool-1-thread-3 is looping of 3 for task of 6
pool-1-thread-3 is looping of 4 for task of 6
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-2 is looping of 4 for task of 5
pool-1-thread-3 is looping of 5 for task of 6
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 5
pool-1-thread-2 is looping of 6 for task of 5
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-3 is looping of 6 for task of 6
pool-1-thread-2 is looping of 7 for task of 5
pool-1-thread-3 is looping of 7 for task of 6
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-3 is looping of 8 for task of 6
pool-1-thread-2 is looping of 8 for task of 5
pool-1-thread-3 is looping of 9 for task of 6
pool-1-thread-2 is looping of 9 for task of 5
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-2 is looping of 1 for task of 8
pool-1-thread-1 is looping of 1 for task of 9
pool-1-thread-3 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 9
pool-1-thread-2 is looping of 2 for task of 8
pool-1-thread-3 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 9
pool-1-thread-2 is looping of 3 for task of 8
pool-1-thread-3 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 9
pool-1-thread-2 is looping of 4 for task of 8
pool-1-thread-3 is looping of 4 for task of 7
pool-1-thread-2 is looping of 5 for task of 8
pool-1-thread-3 is looping of 5 for task of 7
pool-1-thread-1 is looping of 5 for task of 9
pool-1-thread-2 is looping of 6 for task of 8
pool-1-thread-1 is looping of 6 for task of 9
pool-1-thread-3 is looping of 6 for task of 7
pool-1-thread-3 is looping of 7 for task of 7
pool-1-thread-1 is looping of 7 for task of 9
pool-1-thread-2 is looping of 7 for task of 8
pool-1-thread-3 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 9
pool-1-thread-2 is looping of 8 for task of 8
pool-1-thread-3 is looping of 9 for task of 7
pool-1-thread-2 is looping of 9 for task of 8
pool-1-thread-1 is looping of 9 for task of 9

定長線程池的大小最好根據系統資源進行設置。如Runtime.getRuntime().availableProcessors()

1.2 newFixedThreadPool

創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。

public class ThreadPoolTest {
    public static void main(String[] args) {
        //固定線程池
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for (int i = 1; i < 10; i++) {
            int finalI = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j < 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI

                        );

                    }
                }
            });
        }

    }
}
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-1 is looping of 1 for task of 2
pool-1-thread-1 is looping of 2 for task of 2
pool-1-thread-1 is looping of 3 for task of 2
pool-1-thread-1 is looping of 4 for task of 2
pool-1-thread-1 is looping of 5 for task of 2
pool-1-thread-1 is looping of 6 for task of 2
pool-1-thread-1 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 2
pool-1-thread-1 is looping of 1 for task of 3
pool-1-thread-1 is looping of 2 for task of 3
pool-1-thread-1 is looping of 3 for task of 3
pool-1-thread-1 is looping of 4 for task of 3
pool-1-thread-1 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 3
pool-1-thread-1 is looping of 7 for task of 3
pool-1-thread-1 is looping of 8 for task of 3
pool-1-thread-1 is looping of 9 for task of 3
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-1 is looping of 1 for task of 5
pool-1-thread-1 is looping of 2 for task of 5
pool-1-thread-1 is looping of 3 for task of 5
pool-1-thread-1 is looping of 4 for task of 5
pool-1-thread-1 is looping of 5 for task of 5
pool-1-thread-1 is looping of 6 for task of 5
pool-1-thread-1 is looping of 7 for task of 5
pool-1-thread-1 is looping of 8 for task of 5
pool-1-thread-1 is looping of 9 for task of 5
pool-1-thread-1 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 6
pool-1-thread-1 is looping of 4 for task of 6
pool-1-thread-1 is looping of 5 for task of 6
pool-1-thread-1 is looping of 6 for task of 6
pool-1-thread-1 is looping of 7 for task of 6
pool-1-thread-1 is looping of 8 for task of 6
pool-1-thread-1 is looping of 9 for task of 6
pool-1-thread-1 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 7
pool-1-thread-1 is looping of 5 for task of 7
pool-1-thread-1 is looping of 6 for task of 7
pool-1-thread-1 is looping of 7 for task of 7
pool-1-thread-1 is looping of 8 for task of 7
pool-1-thread-1 is looping of 9 for task of 7
pool-1-thread-1 is looping of 1 for task of 8
pool-1-thread-1 is looping of 2 for task of 8
pool-1-thread-1 is looping of 3 for task of 8
pool-1-thread-1 is looping of 4 for task of 8
pool-1-thread-1 is looping of 5 for task of 8
pool-1-thread-1 is looping of 6 for task of 8
pool-1-thread-1 is looping of 7 for task of 8
pool-1-thread-1 is looping of 8 for task of 8
pool-1-thread-1 is looping of 9 for task of 8
pool-1-thread-1 is looping of 1 for task of 9
pool-1-thread-1 is looping of 2 for task of 9
pool-1-thread-1 is looping of 3 for task of 9
pool-1-thread-1 is looping of 4 for task of 9
pool-1-thread-1 is looping of 5 for task of 9
pool-1-thread-1 is looping of 6 for task of 9
pool-1-thread-1 is looping of 7 for task of 9
pool-1-thread-1 is looping of 8 for task of 9
pool-1-thread-1 is looping of 9 for task of 9

結果依次輸出,相當于順序執行各個任務。
現行大多數GUI程序都是單線程的。Android中單線程可用于數據庫操作,文件操作,應用批量安裝,應用批量刪除等不適合并發但可能IO阻塞性及影響UI線程響應的操作。

1.3 newCachedThreadPool

創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程

public class ThreadPoolTest {
    public static void main(String[] args) {
        //固定線程池
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 1; i < 10; i++) {
            int finalI = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j < 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI

                        );

                    }
                }
            });
        }

    }
}
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-8 is looping of 1 for task of 8
pool-1-thread-9 is looping of 1 for task of 9
pool-1-thread-6 is looping of 1 for task of 6
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-7 is looping of 1 for task of 7
pool-1-thread-4 is looping of 1 for task of 4
pool-1-thread-5 is looping of 1 for task of 5
pool-1-thread-6 is looping of 2 for task of 6
pool-1-thread-5 is looping of 2 for task of 5
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-7 is looping of 2 for task of 7
pool-1-thread-8 is looping of 2 for task of 8
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-9 is looping of 2 for task of 9
pool-1-thread-4 is looping of 2 for task of 4
pool-1-thread-5 is looping of 3 for task of 5
pool-1-thread-9 is looping of 3 for task of 9
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-8 is looping of 3 for task of 8
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-7 is looping of 3 for task of 7
pool-1-thread-6 is looping of 3 for task of 6
pool-1-thread-4 is looping of 3 for task of 4
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-6 is looping of 4 for task of 6
pool-1-thread-4 is looping of 4 for task of 4
pool-1-thread-7 is looping of 4 for task of 7
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-9 is looping of 4 for task of 9
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-5 is looping of 4 for task of 5
pool-1-thread-8 is looping of 4 for task of 8
pool-1-thread-6 is looping of 5 for task of 6
pool-1-thread-9 is looping of 5 for task of 9
pool-1-thread-5 is looping of 5 for task of 5
pool-1-thread-8 is looping of 5 for task of 8
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-7 is looping of 5 for task of 7
pool-1-thread-4 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-6 is looping of 6 for task of 6
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-4 is looping of 6 for task of 4
pool-1-thread-7 is looping of 6 for task of 7
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-5 is looping of 6 for task of 5
pool-1-thread-8 is looping of 6 for task of 8
pool-1-thread-9 is looping of 6 for task of 9
pool-1-thread-4 is looping of 7 for task of 4
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-5 is looping of 7 for task of 5
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-9 is looping of 7 for task of 9
pool-1-thread-8 is looping of 7 for task of 8
pool-1-thread-7 is looping of 7 for task of 7
pool-1-thread-6 is looping of 7 for task of 6
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-9 is looping of 8 for task of 9
pool-1-thread-5 is looping of 8 for task of 5
pool-1-thread-7 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-4 is looping of 8 for task of 4
pool-1-thread-8 is looping of 8 for task of 8
pool-1-thread-6 is looping of 8 for task of 6
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-5 is looping of 9 for task of 5
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-4 is looping of 9 for task of 4
pool-1-thread-6 is looping of 9 for task of 6
pool-1-thread-8 is looping of 9 for task of 8
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-7 is looping of 9 for task of 7
pool-1-thread-9 is looping of 9 for task of 9

線程池為無限大,當執行第二個任務時第一個任務已經完成,會復用執行第一個任務的線程,而不用每次新建線程。

1.4 newScheduledThreadPool
public class ThreadPoolTest {
    public static void main(String[] args) {

        System.out.println("init");

        Executors.newScheduledThreadPool(3).schedule(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Done");
                    }
                }, 3, TimeUnit.SECONDS
        );
    }
}
init
Done

表示延遲3秒執行。

定期執行
public class ThreadPoolTest {
    public static void main(String[] args) {

        Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {

                System.out.println("每三秒執行一次");
            }
        },2,3,TimeUnit.SECONDS);

    }
}
每三秒執行一次
每三秒執行一次
每三秒執行一次
每三秒執行一次
省略...

2 關閉線程池

shutdown()

public class ThreadPoolTest {
    public static void main(String[] args) {

        //固定線程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        //緩存線程池 池中的線程數量
        //ExecutorService threadPool = Executors.newCachedThreadPool();

        //單個線程
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();

        for (int i = 1; i < 10; i++) {
            int finalI = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j < 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI

                        );

                    }
                }
            });
        }
        threadPool.shutdown();


    }
}
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-3 is looping of 1 for task of 5
pool-1-thread-2 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-3 is looping of 2 for task of 5
pool-1-thread-2 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-3 is looping of 3 for task of 5
pool-1-thread-2 is looping of 3 for task of 6
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-3 is looping of 4 for task of 5
pool-1-thread-2 is looping of 4 for task of 6
pool-1-thread-3 is looping of 5 for task of 5
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 6
pool-1-thread-3 is looping of 6 for task of 5
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-2 is looping of 6 for task of 6
pool-1-thread-3 is looping of 7 for task of 5
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-2 is looping of 7 for task of 6
pool-1-thread-3 is looping of 8 for task of 5
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-2 is looping of 8 for task of 6
pool-1-thread-3 is looping of 9 for task of 5
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-2 is looping of 9 for task of 6
pool-1-thread-1 is looping of 1 for task of 8
pool-1-thread-2 is looping of 1 for task of 9
pool-1-thread-3 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 8
pool-1-thread-2 is looping of 2 for task of 9
pool-1-thread-3 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 8
pool-1-thread-2 is looping of 3 for task of 9
pool-1-thread-3 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 8
pool-1-thread-2 is looping of 4 for task of 9
pool-1-thread-3 is looping of 4 for task of 7
pool-1-thread-2 is looping of 5 for task of 9
pool-1-thread-3 is looping of 5 for task of 7
pool-1-thread-1 is looping of 5 for task of 8
pool-1-thread-2 is looping of 6 for task of 9
pool-1-thread-3 is looping of 6 for task of 7
pool-1-thread-1 is looping of 6 for task of 8
pool-1-thread-2 is looping of 7 for task of 9
pool-1-thread-3 is looping of 7 for task of 7
pool-1-thread-1 is looping of 7 for task of 8
pool-1-thread-3 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 8
pool-1-thread-2 is looping of 8 for task of 9
pool-1-thread-3 is looping of 9 for task of 7
pool-1-thread-1 is looping of 9 for task of 8
pool-1-thread-2 is looping of 9 for task of 9

shutdownNow

public class ThreadPoolTest {
    public static void main(String[] args) {

        //固定線程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        //緩存線程池 池中的線程數量
        //ExecutorService threadPool = Executors.newCachedThreadPool();

        //單個線程
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();

        for (int i = 1; i < 10; i++) {
            int finalI = i;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 1; j < 10; j++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI

                        );

                    }
                }
            });
        }
        threadPool.shutdownNow();


    }
}
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
java.lang.InterruptedException: sleep interrupted
pool-1-thread-3 is looping of 1 for task of 3
    at java.lang.Thread.sleep(Native Method)
    at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
pool-1-thread-2 is looping of 1 for task of 2
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
2.1 shutdown和shutdownNow的區別

shutdown只是將線程池的狀態設置為SHUTWDOWN狀態,正在執行的任務會繼續執行下去,沒有被執行的則中斷。而shutdownNow則是將線程池的狀態設置為STOP,正在執行的任務則被停止,沒被執行任務的則返回。

2.3 源碼分析shutdown和shutdownNow

1.shutdown源碼

    public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(SHUTDOWN);
            interruptIdleWorkers();
            onShutdown(); // hook for ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
    }

2.shutdownNow源碼

    public List<Runnable> shutdownNow() {
        List<Runnable> tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
            advanceRunState(STOP);
            interruptWorkers();
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
        tryTerminate();
        return tasks;
    }

從源碼可以很清晰的看出兩者的區別,shutdown使用了以后會置狀態為SHUTDOWN,而shutdownNow為STOP。此外,shutdownNow會返回任務列表。

2.3 線程狀態知識延伸

在ThreadPoolExecutor中定義了關于線程狀態的幾個變量如下:

// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;

1.當創建線程池后,初始時,線程池處于RUNNING狀態,此時線程池中的任務為0;
2.如果調用了shutdown()方法,則線程池處于SHUTDOWN狀態,此時線程池不能夠接受新的任務,它會等待所有任務執行完畢;
3.如果調用了shutdownNow()方法,則線程池處于STOP狀態,此時線程池不能接受新的任務,并且會去嘗試終止正在執行的任務;
4.當所有的任務已終止,ctl記錄的”任務數量”為0,線程池會變為TIDYING狀態。接著會執行terminated()函數。
5.線程池處在TIDYING狀態時,執行完terminated()之后,就會由 TIDYING -> TERMINATED,線程池被設置為TERMINATED狀態。

總結

線程池的作用:

線程池作用就是限制系統中執行線程的數量。
根據系統的環境情況,可以自動或手動設置線程數量,達到運行的最佳效果;少了浪費了系統資源,多了造成系統擁擠效率不高。用線程池控制線程數量,其他線程排隊等候。一個任務執行完畢,再從隊列的中取最前面的任務開始執行。若隊列中沒有等待進程,線程池的這一資源處于等待。當一個新任務需要運行時,如果線程池 中有等待的工作線程,就可以開始運行了;否則進入等待隊列。

為什么要用線程池:

1.減少了創建和銷毀線程的次數,每個工作線程都可以被重復利用,可執行多個任務。
2.可以根據系統的承受能力,調整線程池中工作線線程的數目,防止因為消耗過多的內存,而把服務器累趴下(每個線程需要大約1MB內存,線程開的越多,消耗的內存也就越大,最后死機)。

特別感謝:

星辰之力Java 四種線程池
一句話說明白Java線程池中shutdown和shutdownNow的區別

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容