【发布时间】:2018-08-27 04:05:47
【问题描述】:
我有一个场景,执行器线程池中的 n 个线程调用不同的 Web 服务,但所有线程都应在指定的时间限制内完成其任务,否则所有池线程应退出处理其任务并返回其执行器池。
我编写的代码运行良好,但唯一困扰我的情况是我无法取消已经启动的任务,即等待外部 Web 服务响应的线程。
我了解 future.cancel(true) 无法帮助我实现这一目标,但有什么方法可以实现我想要的吗?
我无法通过池线程无限期地等待 Web 服务响应。提前致谢!
public class CallProductServiceTask implements Callable<Object>{
public Object call(){
// Call to product service
// For SOAP Client
ProductRequest productRequest = new ProductRequest("prodId");
ProductResponse response = (ProductResponse)webServiceTemplate.marshalSendAndReceive(productRequest);
// For REST Client
ResponseEntity<ProductResponse> response = restTemplate.exchange(productResourceUrl, HttpMethod.POST, productRequest, ProductResponse.class);
}
}
class Test{
ExecutorService executor = Executors.newFixedThreadPool(2);
public static void main(String args[]){
Future ft = executor.submit(new CallProductServiceTask());
try{
Object result = ft.get(3, TimeUnit.SECONDS);
} catch (TimeoutException e) {
boolean c = future.cancel(true);
System.out.println("Timeout " + c);
} catch (InterruptedException | ExecutionException e) {
System.out.println("interrupted");
}
System.out.println("END");
}
}
【问题讨论】:
-
// Call to product service需要可中断。 -
// Call to product service长什么样子? -
就像这里显示的那样。 // 对于 SOAP 客户端 ProductRequest productRequest = new ProductRequest("prodId"); ProductResponse 响应 = (ProductResponse) webServiceTemplate.marshalSendAndReceive(productRequest); // 对于 REST 客户端 ResponseEntity
response = restTemplate.exchange(productResourceUrl, HttpMethod.POST, productRequest, ProductResponse.class); -
如果我的设计不正确,或者有其他正确的方法可以满足这种情况,请告诉我?
-
如果你使用的是posix线程,应该有一个取消函数在线程id上执行,例如在C中有一个
pthread_cancel( pthread_t *tid),这个函数取消一个线程的执行
标签: java multithreading future executorservice threadpoolexecutor