任务不等待ThreadPool上可用的线程

问题描述

基本上我已经实现了以下简单代码:

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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...