gpt4 book ai didi

java - 如何使用 CompletionService 取消那些花费太长时间的任务

转载 作者:搜寻专家 更新时间:2023-10-31 19:49:46 26 4
gpt4 key购买 nike

我使用包裹在 2 线程 FixedThreadPool ExecutorService 周围的 CompletionService 提交了一些 Future 任务,然后我设置了一个等于提交任务数量的循环,并使用 completionservice.take() 等待它们全部完成或失败。问题是偶尔它永远不会完成(但我不知道为什么)所以我将 take() 方法更改为 poll(300,Timeout.SECONDS),这个想法是如果一个任务需要超过 5 分钟才能完成poll 会失败,然后最终会跳出循环,我可以遍历所有 futures 并调用 future.cancel(true) 强制取消有问题的任务。

但是当我运行代码并且它挂起时,我看到轮询每 5 分钟连续失败一次并且没有更多任务运行所以我假设这两个工作人员以某种方式陷入僵局并且永远不会完成,并且永远不会允许额外的任务开始。因为超时是 5 分钟,还有 1000 个任务要运行,打破循环所花费的时间太长,所以取消了作业。

所以我想做的是,如果 5 分钟内没有完成,则中断/强制取消当前任务,但我看不到任何方法。

此代码示例显示了我所说内容的简化版本

import com.jthink.jaikoz.exception.JaikozException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

public class CompletionServiceTest
{
public static void main(final String[] args)
{
CompletionService<Boolean> cs = new ExecutorCompletionService<Boolean>(Executors.newFixedThreadPool(2));
Collection<Worker> tasks = new ArrayList<Worker>(10);
tasks.add(new Worker(1));
tasks.add(new Worker(2));
tasks.add(new Worker(3));
tasks.add(new Worker(4));
tasks.add(new Worker(5));
tasks.add(new Worker(6));

List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>(tasks.size());
try
{
for (Callable task : tasks)
{
futures.add(cs.submit(task));
}
for (int t = 0; t < futures.size(); t++)
{
Future<Boolean> result = cs.poll(10, TimeUnit.SECONDS);
if(result==null)
{
System.out.println("Worker TimedOut:");
continue;
}
else
{
try
{
if(result.isDone() && result.get())
{
System.out.println("Worker Completed:");
}
else
{
System.out.println("Worker Failed");
}
}
catch (ExecutionException ee)
{
ee.printStackTrace();
}
}
}
}
catch (InterruptedException ie)
{
}
finally
{
//Cancel by interrupting any existing tasks currently running in Executor Service
for (Future<Boolean> f : futures)
{
f.cancel(true);
}
}
System.out.println("Done");
}
}

class Worker implements Callable<Boolean>
{
private int number;
public Worker(int number)
{
this.number=number;
}

public Boolean call()
{
if(number==3)
{
try
{
Thread.sleep(50000);
}
catch(InterruptedException tie)
{

}
}
return true;
}
}

输出

Worker Completed:
Worker Completed:
Worker Completed:
Worker Completed:
Worker Completed:
Worker TimedOut:
Done

最佳答案

我想我已经解决了,基本上如果发生超时,我会遍历我的 future 对象列表并找到第一个未完成的对象,然后强制取消。看起来不那么优雅,但似乎有效。

我更改了池的大小只是为了显示更好地演示解决方案但也适用于 2 线程池的输出。

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

public class CompletionServiceTest
{
public static void main(final String[] args)
{
CompletionService<Boolean> cs = new ExecutorCompletionService<Boolean>(Executors.newFixedThreadPool(1));
Collection<Worker> tasks = new ArrayList<Worker>(10);
tasks.add(new Worker(1));
tasks.add(new Worker(2));
tasks.add(new Worker(3));
tasks.add(new Worker(4));
tasks.add(new Worker(5));
tasks.add(new Worker(6));

List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>(tasks.size());
try
{
for (Callable task : tasks)
{
futures.add(cs.submit(task));
}
for (int t = 0; t < futures.size(); t++)
{
System.out.println("Invocation:"+t);
Future<Boolean> result = cs.poll(10, TimeUnit.SECONDS);
if(result==null)
{
System.out.println(new Date()+":Worker Timedout:");
//So lets cancel the first futures we find that havent completed
for(Future future:futures)
{
System.out.println("Checking future");
if(future.isDone())
{
continue;
}
else
{
future.cancel(true);
System.out.println("Cancelled");
break;
}
}
continue;
}
else
{
try
{
if(result.isDone() && !result.isCancelled() && result.get())
{
System.out.println(new Date()+":Worker Completed:");
}
else if(result.isDone() && !result.isCancelled() && !result.get())
{
System.out.println(new Date()+":Worker Failed");
}
}
catch (ExecutionException ee)
{
ee.printStackTrace(System.out);
}
}
}
}
catch (InterruptedException ie)
{
}
finally
{
//Cancel by interrupting any existing tasks currently running in Executor Service
for (Future<Boolean> f : futures)
{
f.cancel(true);
}
}
System.out.println(new Date()+":Done");
}
}

class Worker implements Callable<Boolean>
{
private int number;
public Worker(int number)
{
this.number=number;
}

public Boolean call()
throws InterruptedException
{
try
{
if(number==3)
{
Thread.sleep(50000);
}
}
catch(InterruptedException ie)
{
System.out.println("Worker Interuppted");
throw ie;
}
return true;
}
}

输出是

Invocation:0
Thu Mar 10 20:51:39 GMT 2011:Worker Completed:
Invocation:1
Thu Mar 10 20:51:39 GMT 2011:Worker Completed:
Invocation:2
Thu Mar 10 20:51:49 GMT 2011:Worker Timedout:
Checking future
Checking future
Checking future
Cancelled
Invocation:3
Worker Interuppted
Invocation:4
Thu Mar 10 20:51:49 GMT 2011:Worker Completed:
Invocation:5
Thu Mar 10 20:51:49 GMT 2011:Worker Completed:
Thu Mar 10 20:51:49 GMT 2011:Done

关于java - 如何使用 CompletionService 取消那些花费太长时间的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5260661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com