考虑以下工人:
public class Worker implements Callable<String> ....
// initialize context
@Override
public String call() {
ZMQ.Socket pullSocket = context.socket(ZMQ.PULL);
pullSocket.connect(HOST_PULL_SOCKET);
while (!Thread.currentThread().isInterrupted() && !context.isClosed()) {
String result = pullSocket.recvStr();
return result;
}
return "test";
}
抛出pullSocket.recvStr()
如下java.nio.channels.ClosedByInterruptException
场景
ExecutorService service = Executors.newFixedThreadPool(4);
List<Worker> workers = new ArrayList<>();
for(int i = 0: i < 4;i++){
workers.add(new Worker()); // Class where call() is executed
}
String result = service.invokeAny(workers);
被执行。
我怎样才能避免这种异常或者在invokeAny()
被调用时处理套接字关闭的首选方法是什么?