java ThreadPoolExecutor 的使用例子
时间: 2023-10-17 17:14:45 浏览: 60
好的,下面是一个简单的使用例子:
```java
// 创建线程池,包括3个核心线程,最大线程数为5,线程池缓冲队列容量为100
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 5, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
// 提交任务
for (int i = 0; i < 10; i++) {
final int taskId = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("当前线程 " + Thread.currentThread().getName() + " 执行任务 " + taskId);
}
});
}
// 关闭线程池
threadPool.shutdown();
```
以上是一个简单的Java ThreadPoolExecutor 使用例子,其中创建了一个包含3个核心线程、最大线程数为5、缓冲队列容量为100的线程池,并且提交了10个任务。每个任务就是输出当前线程的名字和任务ID。
需要注意的是,最后要调用 `shutdown` 方法来关闭线程池。
相关问题
Java threadpoolexecutor使用例子
Java中的ThreadPoolExecutor是一个线程池的实现类,它提供了一种方便的方式来管理和重用线程。下面是一个ThreadPoolExecutor的使用例子:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadPoolExecutorExample {
public static void main(String[] args) {
// 创建一个线程池,包含10个线程
ExecutorService executorService = Executors.newFixedThreadPool(10);
// 将线程池强制转换为ThreadPoolExecutor类型
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
// 设置线程池的属性
threadPoolExecutor.setCorePoolSize(5);
threadPoolExecutor.setMaximumPoolSize(10);
threadPoolExecutor.setKeepAliveTime(5000, TimeUnit.MILLISECONDS);
// 执行任务
for (int i = 0; i < 20; i++) {
executorService.execute(new Task(i));
}
// 关闭线程池
executorService.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.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Task " + taskId + " is completed.");
}
}
}
```
在上面的例子中,我们创建了一个包含10个线程的线程池,然后将其强制转换为ThreadPoolExecutor类型,并设置了线程池的属性。接着,我们执行了20个任务,每个任务都是一个Runnable对象,最后关闭了线程池。
注意,我们可以通过调用execute方法来提交任务到线程池中,而不需要手动创建线程。线程池会自动管理和重用线程,可以更加高效地利用系统资源。
java ThreadPoolExecutor 使用示例,请给出详细示例代码
好的,以下是一个简单的 Java ThreadPoolExecutor 使用示例:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
// 创建一个大小为 5 的线程池
ExecutorService executor = Executors.newFixedThreadPool(5);
// 向线程池提交 10 个任务
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("Task " + i);
executor.execute(worker);
}
// 关闭线程池
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all tasks");
}
}
class WorkerThread implements Runnable {
private String taskName;
public WorkerThread(String taskName) {
this.taskName = taskName;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " Start. Task = " + taskName);
processTask();
System.out.println(Thread.currentThread().getName() + " End.");
}
private void processTask() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getTaskName() {
return this.taskName;
}
}
```
在这个示例中,我们创建了一个大小为 5 的线程池。然后向线程池提交了 10 个任务,每个任务都是一个实现了 Runnable 接口的 WorkerThread 对象。在每个工作线程中,我们打印了线程名以及任务名,并模拟了处理任务的过程。最后,我们关闭了线程池,并等待所有任务完成后输出“Finished all tasks”消息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)