1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package J20250731.demo03_threadPool;
- import java.util.concurrent.*;
- /**
- * @author WanJl
- * @version 1.0
- * @title ThreadPoolDemo03
- * @description
- * @create 2025/7/31
- */
- public class ThreadPoolDemo03 {
- public static void main(String[] args) {
- /*
- 1、核心线程数
- 2、最大线程数
- 3、空闲线程最大存活时间
- 4、时间单位
- 5、任务队列
- 6、线程工厂
- 7、任务拒绝策略
- */
- ThreadPoolExecutor pool =
- new ThreadPoolExecutor(2,
- 5,
- 2,
- TimeUnit.SECONDS, new ArrayBlockingQueue<>(10),
- Executors.defaultThreadFactory(),
- new ThreadPoolExecutor.AbortPolicy());
- for (int i = 0; i < 100; i++) {
- try {
- pool.submit(() -> {
- System.out.println("线程id:" + Thread.currentThread().getId() + ",线程名:" + Thread.currentThread().getName() + "在执行了.....");
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- });
- } catch (RejectedExecutionException e) {
- System.out.println("一桌客人被撵跑了....");
- }
- }
- System.out.println("线程池的线程数:" + pool.getPoolSize());
- pool.shutdown();
- }
- }
|