
- Java 併發教程
- 併發 - 首頁
- 併發 - 概述
- 併發 - 環境搭建
- 併發 - 主要操作
- 執行緒間通訊
- 併發 - 同步
- 併發 - 死鎖
- 工具類示例
- 併發 - ThreadLocal
- 併發 - ThreadLocalRandom
- 鎖示例
- 併發 - 鎖
- 併發 - 讀寫鎖
- 併發 - 條件
- 原子變數示例
- 併發 - AtomicInteger
- 併發 - AtomicLong
- 併發 - AtomicBoolean
- 併發 - AtomicReference
- 併發 - AtomicIntegerArray
- 併發 - AtomicLongArray
- 併發 - AtomicReferenceArray
- 執行器示例
- 併發 - Executor
- 併發 - ExecutorService
- ScheduledExecutorService
- 執行緒池示例
- 併發 - newFixedThreadPool
- 併發 - newCachedThreadPool
- newScheduledThreadPool
- newSingleThreadExecutor
- 併發 - ThreadPoolExecutor
- ScheduledThreadPoolExecutor
- 高階示例
- 併發 - Futures 和 Callables
- 併發 - Fork-Join 框架
- 併發集合
- 併發 - BlockingQueue
- 併發 - ConcurrentMap
- ConcurrentNavigableMap
- 併發實用資源
- 併發 - 快速指南
- 併發 - 有用資源
- 併發 - 討論
newFixedThreadPool 方法
可以透過呼叫 Executors 類的靜態 newFixedThreadPool() 方法來獲取固定大小的執行緒池。
語法
ExecutorService fixedPool = Executors.newFixedThreadPool(2);
其中
最多將有 2 個執行緒同時處理任務。
如果提交的任務超過 2 個,則這些任務將被放入佇列中,等待執行緒可用。
如果某個執行緒由於執行過程中出現故障而終止,並且尚未呼叫 executor 的關閉操作,則會建立一個新執行緒來代替它。
任何執行緒都會一直存在,直到執行緒池關閉。
示例
下面的 TestThread 程式演示了在基於執行緒的環境中使用 newFixedThreadPool 方法。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class TestThread { public static void main(final String[] arguments) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(2); // Cast the object to its class type ThreadPoolExecutor pool = (ThreadPoolExecutor) executor; //Stats before tasks execution System.out.println("Largest executions: " + pool.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + pool.getMaximumPoolSize()); System.out.println("Current threads in pool: " + pool.getPoolSize()); System.out.println("Currently executing threads: " + pool.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + pool.getTaskCount()); executor.submit(new Task()); executor.submit(new Task()); //Stats after tasks execution System.out.println("Core threads: " + pool.getCorePoolSize()); System.out.println("Largest executions: " + pool.getLargestPoolSize()); System.out.println("Maximum allowed threads: " + pool.getMaximumPoolSize()); System.out.println("Current threads in pool: " + pool.getPoolSize()); System.out.println("Currently executing threads: " + pool.getActiveCount()); System.out.println("Total number of threads(ever scheduled): " + pool.getTaskCount()); executor.shutdown(); } static class Task implements Runnable { public void run() { try { Long duration = (long) (Math.random() * 5); System.out.println("Running Task! Thread Name: " + Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(duration); System.out.println("Task Completed! Thread Name: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
這將產生以下結果。
輸出
Largest executions: 0 Maximum allowed threads: 2 Current threads in pool: 0 Currently executing threads: 0 Total number of threads(ever scheduled): 0 Core threads: 2 Largest executions: 2 Maximum allowed threads: 2 Current threads in pool: 2 Currently executing threads: 1 Total number of threads(ever scheduled): 2 Running Task! Thread Name: pool-1-thread-1 Running Task! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-2 Task Completed! Thread Name: pool-1-thread-1
廣告