java.util.concurrent.ThreadPoolExecutor 测试类
时间: 2023-11-21 09:57:38 浏览: 180
以下是一个简单的java.util.concurrent.ThreadPoolExecutor测试类的例子:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolExecutorTest {
public static void main(String[] args) {
// 创建一个线程池
ExecutorService executorService = Executors.newFixedThreadPool(5);
// 将线程池转换为ThreadPoolExecutor类型
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
// 执行任务
for (int i = 0; i < 10; i++) {
threadPoolExecutor.execute(new Task(i));
}
// 关闭线程池
threadPoolExecutor.shutdown();
}
static class Task implements Runnable {
private int taskId;
public Task(int taskId) {
this.taskId = taskId;
}
@Override
public void run() {
System.out.println("Task " + taskId + " is running.");
}
}
}
```
阅读全文