问题描述
基本上我已经实现了以下简单代码:
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中的任务...
我希望能够执行任务,但由于池中的线程可用,因此我希望为他们提供服务,这样我就不会耗尽资源。
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)