ThreadPoolExecutor使用方法

先看構造方法 ,ThreadPoolExecutor共4個構造方法:

ThreadPoolExecutor使用方法

直接看參數最多的7個參數分別代表:

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
/<runnable>
  • corePoolSize: 線程池核心線程數
  • maximumPoolSize:線程池最大數
  • keepAliveTime: 空閒線程存活時間
  • unit: 時間單位
  • workQueue: 線程池所使用的緩衝隊列
  • threadFactory:線程池創建線程使用的工廠
  • handler: 線程池對拒絕任務的處理策略

1.當池中正在運行的線程數(包括空閒線程數)小於corePoolSize時,新建線程執行任務

 public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
// 任務1
pool.execute(() -> {
try {
Thread.sleep(3 * 1000);
System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
//任務2
pool.execute(() -> System.out.println("--helloWorld_002--" + Thread.currentThread().getName()));
}
ThreadPoolExecutor使用方法

結論:線程1 結束後 沒有繼續線程1 而是啟動線程2

2.當池中正在運行的線程數(包括空閒線程數)大於等於corePoolSize時,新插入的任務進入workQueue排隊(如果workQueue長度允許),等待空閒線程來執行。

 public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
// 任務1
pool.execute(() -> {
try {
Thread.sleep(3 * 1000);
System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務2
pool.execute(() -> {
try {
Thread.sleep(5 * 1000);
System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務3
pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
}
ThreadPoolExecutor使用方法

結論:任務2在運行過程中,任務3啟動不會新建線程,因為有一個隊列是空的,maximumPoolSize=3這個參數不起作用。

3.當隊列裡的任務達到上限,並且池中正在進行的線程小於maxinumPoolSize,對於新加入的任務,新建線程。

 public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
// 任務1
pool.execute(() -> {
try {
Thread.sleep(3 * 1000);
System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務2
pool.execute(() -> {
try {
Thread.sleep(5 * 1000);
System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務3
pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
// 任務4
pool.execute(() -> System.out.println("--helloWorld_004--" + Thread.currentThread().getName()));
}
ThreadPoolExecutor使用方法

結果:任務1,2啟動後 任務3在隊列 ,隊列就滿了,由於正在進行的線程數是2<maximumpoolsize>

4.隊列裡的任務達到上限,並且池中正在運行的線程等於maximumPoolSize,對於新加入的任務,執行拒絕策略(線程池默認的策略是拋異常)。

public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
// 任務1
pool.execute(() -> {
try {
Thread.sleep(3 * 1000);
System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務2
pool.execute(() -> {
try {
Thread.sleep(5 * 1000);
System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// 任務3
pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
// 任務4
pool.execute(() -> {
try {
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("--helloWorld_004--" + Thread.currentThread().getName());
});
// 任務5
pool.execute(() -> System.out.println("--helloWorld_005--" + Thread.currentThread().getName()));
}

運行結果:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task ExecutorDemo$$Lambda$5/999966131@7699a589 rejected from java.util.concurrent.ThreadPoolExecutor@58372a00[Running, pool size = 3, active threads = 3, queued tasks = 1, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at ExecutorDemo.main(ExecutorDemo.java:40)
--helloWorld_004----pool-1-thread-3
--helloWorld_003--pool-1-thread-3
--helloWorld_001--pool-1-thread-1
--helloWorld_002--pool-1-thread-2

結論:隊列達到上限,線程池達到最大值,故拋出異常。

ThreadPoolExecutor使用方法

/<maximumpoolsize>


分享到:


相關文章: