java線程池

一:線程池是一種多線程處理形式,處理過程當中將任務添加到隊列,而後在建立線程後自動啓動這些任務。線程池線程都是後臺線程。每一個線程都使用默認的堆棧大小,以默認的優先級運行,並處於多線程單元中。java

二:使用線程池的好處:多線程

(1):下降資源消耗,經過重複利用已建立的線程下降線程建立和銷燬形成的消耗。spa

(2):提升響應速度,當任務到達時,任務能夠不須要等到線程建立就能當即執行。線程

(3):提升線程的可管理性,線程是稀缺資源,若是無限制的建立,不只會消耗系統資源,還會下降系統的穩定性,使用線程池能夠進行統一的分配,調優和監控code

三:使用代碼實現線程池:blog

class Test1{
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
              //核心線程數
                3,
                //最大線程數
                5,
                //存活時間
                1L,
                //時間單位
                TimeUnit.SECONDS,
                //等待隊列
                new ArrayBlockingQueue<>(3),
                //線程工廠
                Executors.defaultThreadFactory(),
                //拒絕策略
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<3;i++){
            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName() + "===> 執行任務");
            });
        }
    }
}

  

 

 此時正常分配任務隊列

class Test1{
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
              //核心線程數
                3,
                //最大線程數
                5,
                //存活時間
                1L,
                //時間單位
                TimeUnit.SECONDS,
                //等待隊列
                new ArrayBlockingQueue<>(3),
                //線程工廠
                Executors.defaultThreadFactory(),
                //拒絕策略
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<6;i++){
            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName() + "===> 執行任務");
            });
        }
    }

 

 此時須要執行的任務超過了核心線程數,則會讓任務進入等待隊列,等前面的任務執行完而後執行.資源

class Test1{
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
              //核心線程數
                3,
                //最大線程數
                5,
                //存活時間
                1L,
                //時間單位
                TimeUnit.SECONDS,
                //等待隊列
                new ArrayBlockingQueue<>(3),
                //線程工廠
                Executors.defaultThreadFactory(),
                //拒絕策略
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<8;i++){
            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName() + "===> 執行任務");
            });
        }
    }
}

 

 此時須要執行的任務超過了核心線程數,等待隊列滿了後,線程池會建立不超過最大線程數的線程來執行任務.get

class Test1{
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
              //核心線程數
                3,
                //最大線程數
                5,
                //存活時間
                1L,
                //時間單位
                TimeUnit.SECONDS,
                //等待隊列
                new ArrayBlockingQueue<>(3),
                //線程工廠
                Executors.defaultThreadFactory(),
                //拒絕策略
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<11;i++){
            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName() + "===> 執行任務");
            });
        }
    }
}

 

 此時須要執行的任務超過了最大線程數,它會把可以執行的任務執行而後報錯.it

相關文章
相關標籤/搜索