java ThreadPoolExecutor 的使用例子
时间: 2023-10-17 17:14:45 浏览: 122
好的,下面是一个简单的使用例子:
```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 使用示例,请给出详细示例代码
示例代码如下:import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;public class ThreadPoolExecutorExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executorService.execute(worker);
}
executorService.shutdown();
while (!executorService.isTerminated()) {
}
System.out.println("Finished all threads");
}
}class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s){
this.message=s;
}
public void run() {
System.out.println(Thread.currentThread().getName()+" (Start) message = "+message);
processmessage();//call processmessage method that sleeps the thread for 2 seconds
System.out.println(Thread.currentThread().getName()+" (End)");//prints thread name
}
private void processmessage() {
try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
}
}
阅读全文