写一段创建java线程池的方法
时间: 2024-01-29 17:01:47 浏览: 69
好的,下面是创建Java线程池的方法:
```java
public class ThreadPoolExample {
public static void main(String[] args) {
// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(10);
// 提交任务到线程池
for (int i = 0; i < 100; i++) {
executor.execute(new Runnable() {
public void run() {
// 执行任务
}
});
}
// 关闭线程池
executor.shutdown();
}
}
```
上面的代码中,我们使用了`Executors.newFixedThreadPool()`方法来创建一个固定大小的线程池,大小为10。然后,我们使用`executor.execute()`方法将任务提交到线程池中执行。最后,我们使用`executor.shutdown()`方法关闭线程池。
相关问题
java线程池的创建
### Java线程池创建与使用
在Java中,`java.util.concurrent`包提供了强大的工具用于管理和优化多线程应用。为了高效地利用资源并简化并发编程模型,推荐采用内置的线程池机制而不是手动实例化Thread对象。
#### 创建固定大小的线程池
通过调用`Executors.newFixedThreadPool(int nThreads)`可以方便快捷地构建具有指定数目工作线程的线程池[^2]:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; ++i) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {}
System.out.println("Finished all threads");
}
private static class WorkerThread implements Runnable {
private final String command;
public WorkerThread(String s) {
this.command = s;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " Start. Command = " + command);
processCommand();
System.out.println(Thread.currentThread().getName() + " End.");
}
private void processCommand() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
这段代码展示了如何定义一个包含十个工作者线程的线程池,并向其提交一百个任务。每个任务由实现了`Runnable`接口的对象表示,在这里即为内部静态类`WorkerThread`。当所有的工作都完成后,主线程会等待直到所有的子线程结束才继续执行下去[^3]。
对于更复杂的场景,则可以直接使用`ThreadPoolExecutor`构造函数来自定义更多参数,比如最大允许的任务队列长度、保持活动的时间等设置[^4]。
java 写一段动态线程池案例
例如:import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class DynamicThreadPool {
public static void main (String[] args) {
// 创建一个可以根据需要创建新线程的线程池
ExecutorService executor = Executors.newCachedThreadPool();
// 向线程池提交10个任务
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
}
// 关闭线程池
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)