Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中Do While循环的ExecutorService_Java_Multithreading_Try Catch_Do While_Executorservice - Fatal编程技术网

Java中Do While循环的ExecutorService

Java中Do While循环的ExecutorService,java,multithreading,try-catch,do-while,executorservice,Java,Multithreading,Try Catch,Do While,Executorservice,我对并发性是新手,我试图为do-while循环实现executor服务并发性。但我总是遇到RejectedExecutionException 以下是我的示例代码: do { Future<Void> future = executor.submit(new Callable<Void>() { @Override public Void call() throws Exception { // actio

我对并发性是新手,我试图为do-while循环实现executor服务并发性。但我总是遇到
RejectedExecutionException

以下是我的示例代码:

do {    
    Future<Void> future = executor.submit(new Callable<Void>() {
    @Override        
    public Void call() throws Exception {

        // action
        return null;
    }
    });

    futures.add(future);
    executor.shutdown();

    for (Future<Void> future : futures) {
        try {
            future.get();
        }
        catch (InterruptedException e) {
            throw new IOException(e)
        }          
    } 
}
while (true);
do{
Future=executor.submit(new Callable()){
@凌驾
public Void call()引发异常{
//行动
返回null;
}
});
期货。添加(期货);
executor.shutdown();
for(未来:未来){
试一试{
future.get();
}
捕捉(中断异常e){
抛出新IOException(e)
}          
} 
}
虽然(正确);

但这似乎不正确。我想我打错电话了。有人能帮我在do while循环中正确执行执行器服务吗。谢谢。

你说得对,没有在正确的时间调用
shutdown
方法。调用
shutdown
后,
ExecutorService
将不接受任务(除非您实现自己的版本)


您应该在将所有任务提交给执行器后调用
shutdown
,因此在这种情况下,在
do while
循环之后的某个地方。

您是对的,没有在正确的时间调用
shutdown
方法。调用
shutdown
后,
ExecutorService
将不接受任务(除非您实现自己的版本)

您应该在向执行器提交所有任务后调用
shutdown
,因此在这种情况下,在
do while
循环之后的某个地方。

停止
执行器服务
接受更多作业。提交完作业后,应该调用它

也是一个阻塞方法,这意味着它将阻塞当前线程的执行,循环的下一次迭代将不会继续,除非这个未来(调用get)返回。这将在每次迭代中发生,这使得代码非并行

您可以使用等待所有作业返回

以下是正确的代码

final List<Object> results = Collections.synchronizedList(new ArrayList<Object>());
final CountDownLatch latch = new CountDownLatch(10);//suppose you'll have 10 futures

do {
    Future<Void> future = executor.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            // action
            latch.countDown();//decrease the latch count
            results.add(result); // some result
            return null;
        }
    });

    futures.add(future);
} while (true);

executor.shutdown();

latch.await(); //This will block till latch.countDown() has been called 10 times.

//Now results has all the outputs, do what you want with them.
final List results=Collections.synchronizedList(new ArrayList());
最终倒计时闩锁=新倒计时闩锁(10)//假设你有10个期货
做{
Future=executor.submit(new Callable()){
@凌驾
public Void call()引发异常{
//行动
lock.countDown();//减少闩锁计数
results.add(result);//一些结果
返回null;
}
});
期货。添加(期货);
}虽然(正确);
executor.shutdown();
satch.wait()//这将一直阻止,直到调用了10次latch.countDown()。
//现在结果有了所有的输出,你想用它们做什么就做什么。
另外,如果您使用的是Java 8,那么您可以看看这个答案

阻止
执行器服务
接受更多作业。提交完作业后,应该调用它

也是一个阻塞方法,这意味着它将阻塞当前线程的执行,循环的下一次迭代将不会继续,除非这个未来(调用get)返回。这将在每次迭代中发生,这使得代码非并行

您可以使用等待所有作业返回

以下是正确的代码

final List<Object> results = Collections.synchronizedList(new ArrayList<Object>());
final CountDownLatch latch = new CountDownLatch(10);//suppose you'll have 10 futures

do {
    Future<Void> future = executor.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            // action
            latch.countDown();//decrease the latch count
            results.add(result); // some result
            return null;
        }
    });

    futures.add(future);
} while (true);

executor.shutdown();

latch.await(); //This will block till latch.countDown() has been called 10 times.

//Now results has all the outputs, do what you want with them.
final List results=Collections.synchronizedList(new ArrayList());
最终倒计时闩锁=新倒计时闩锁(10)//假设你有10个期货
做{
Future=executor.submit(new Callable()){
@凌驾
public Void call()引发异常{
//行动
lock.countDown();//减少闩锁计数
results.add(result);//一些结果
返回null;
}
});
期货。添加(期货);
}虽然(正确);
executor.shutdown();
satch.wait()//这将阻止,直到调用lock.countDown()10次。
//现在结果有了所有的输出,你想用它们做什么就做什么。
另外,如果您使用的是Java 8,那么您可以从文档中查看以下答案:

拒绝的任务

在方法execute(Runnable)中提交的新任务在执行器关闭时,以及在执行器对最大线程和工作队列容量使用有限边界且已饱和时,将被拒绝

在任何一种情况下,execute方法都会调用其RejectedExecutionHandler的
rejectedExecution(Runnable,ThreadPoolExecutor)
方法

从代码中可以明显看出,您首先调用了
shutdown()
,然后提交任务

另一方面,有关关闭ExecutorService的正确方法,请参阅此相关SE问题:

来自文档:

拒绝的任务

在方法execute(Runnable)中提交的新任务在执行器关闭时,以及在执行器对最大线程和工作队列容量使用有限边界且已饱和时,将被拒绝

在任何一种情况下,execute方法都会调用其RejectedExecutionHandler的
rejectedExecution(Runnable,ThreadPoolExecutor)
方法

从代码中可以明显看出,您首先调用了
shutdown()
,然后提交任务

另一方面,有关关闭ExecutorService的正确方法,请参阅此相关SE问题:


好的观点。我的2美分:除非你想在处理任何一个期货之前等待所有的期货完成,否则你不需要倒计时。重要的是不要给未来打电话;只要调用发生在循环之后,它们可能不会立即返回就可以了;这将产生与等待闩锁类似的效果。@dnault您是