java - 不等待线程池上可用线程的任务
问题描述
基本上我已经实现了这个简单的代码:
public static void main( String[] args ){
int it = 1;
ExecutorService executorService = Executors.newFixedThreadPool(2);
while(true){
for(int i = 0; i < 500; i++){
System.out.println("Exec " + it + " " + i);
executorService.execute(new Runnable(){
@Override
public void run(){
System.out.println("entered");
try{
Thread.sleep(1000);
}catch ( InterruptedException e ){
e.printStackTrace();
}
}
});
}
System.out.println("Finished it "+ it++);
}
}
上面的代码开始向 executorService 添加无限任务,并且不等待线程可用。这意味着我们可以在迭代 N 并且仍然从迭代 1 执行任务......
我希望能够执行这些任务,但我想为它们提供服务,因为池中的线程可用,所以我不会用完资源。
谢谢你。
解决方案
推荐阅读
- flutter - 每当尝试添加我在颤振中删除的相同项目时都会出现异常
- php - 覆盖强制转换方法,无法保存到数据库
- css - 角徽章 - 随着长度的增加改变形状
- reactjs - NextJs - 以对象表示法将 HTML Lang 属性添加到自定义 _document
- spring - 如何计算项目反应堆通量的平均值?
- java - 使用基本身份验证从 Spring Boot 站点将 CSV 加载到 Power Query
- c++ - 无法设置动态数组底部c ++
- reactjs - Redux 连接优化 mergeProps
- c# - 根据日期自动更改值
- javascript - 锚定到 div 以转到 div 上方的某个点以允许固定标题