- 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
广告