java用线程池创建100个线程,core是5,其余95个存在哪里了?
时间: 2024-05-27 20:12:28 浏览: 9
在Java中,线程池可以分为两种类型:固定大小的线程池和可变大小的线程池。
对于固定大小的线程池,其核心线程数和最大线程数是相同的,因此在创建线程池时,所有线程都会被创建并保持活动状态,即使没有任务需要执行,这些线程也会一直存在。因此,在这种情况下,95个线程会一直存在于线程池中,等待任务的到来。
对于可变大小的线程池,其核心线程数和最大线程数是可以动态调整的。在创建线程池时,只有核心线程被创建并保持活动状态,而其他线程则会在任务到达时动态创建。因此,在这种情况下,除了5个核心线程外,其余95个线程并不存在,只有在有任务到达时才会被创建。当任务执行完毕后,这些线程也会被回收。
相关问题
java语言写一个线程池,核心线程为4,最大线程100。和取线程使用的方法
好的,下面是一个简单的 Java 线程池:
```java
import java.util.concurrent.*;
public class MyThreadPool {
private static final int CORE_POOL_SIZE = 4;
private static final int MAXIMUM_POOL_SIZE = 100;
private static final int KEEP_ALIVE_TIME = 60;
private static ExecutorService threadPool = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()
);
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
int taskNumber = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("Task " + taskNumber + " is running.");
}
});
}
threadPool.shutdown();
}
}
```
这个线程池的核心线程数量为 4,最大线程数量为 100,空闲线程存活时间为 60 秒。任务队列使用的是一个无界队列,因此任务可以无限制添加。
在 `main` 方法中,我们向线程池提交了 10 个任务,线程池会自动调度线程来执行这些任务。最后,我们调用 `shutdown` 方法来关闭线程池。
如果你想手动取出线程使用,可以使用 `submit` 方法,示例如下:
```java
Future<String> future = threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "Hello, world!";
}
});
String result = future.get(); // 等待任务执行完毕并获取结果
System.out.println(result);
```
这里我们使用了 `submit` 方法来提交一个 `Callable` 任务,这个任务会返回一个字符串。`submit` 方法会返回一个 `Future` 对象,我们可以用它来获取任务的执行结果。在这里,我们调用了 `future.get()` 方法来等待任务执行完毕并获取结果。
用java写一个动态多线程,带动态线程池的例子
好的,这是一个使用 Java 实现的动态多线程、带动态线程池的示例代码:
```
import java.util.concurrent.*;
public class DynamicThreadPool {
private static final int CORE_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = 10;
private static final int KEEP_ALIVE_TIME = 5;
private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
private static final BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>(5);
private static ThreadPoolExecutor threadPoolExecutor;
public static void main(String[] args) {
threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, WORK_QUEUE);
for (int i = 1; i <= 20; i++) {
final int taskId = i;
threadPoolExecutor.execute(() -> {
System.out.println("Task " + taskId + " is running on " + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("Current pool size: " + threadPoolExecutor.getPoolSize());
}
threadPoolExecutor.shutdown();
}
}
```
在上面的代码中,我们创建了一个线程池,它的核心线程数为 5,最大线程数为 10,空闲线程存活时间为 5 秒,工作队列的大小为 5。每提交一个任务,都会打印当前线程池的大小。
运行代码后,你会发现,当线程池的大小达到核心线程数时,新的任务会被放到工作队列中;当工作队列满了,再有新任务时,线程池会创建新线程来处理任务,直到线程池中的线程数达到最大
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)