我正在计算等待串行事件发生超时的未来:
Future<Response> future = executor.submit(new CommunicationTask(this, request));
response = new Response("timeout");
try {
response = future.get(timeoutMilliseconds, TimeUnit.MILLISECONDS);
} catch (InterruptedException | TimeoutException e) {
future.cancel(true);
log.info("Execution time out." + e);
} catch (ExecutionException e) {
future.cancel(true);
log.error("Encountered problem communicating with device: " + e);
}
Run Code Online (Sandbox Code Playgroud)
该CommunicationTask班实施了Observer监听来自串行端口的变化接口.
问题是从串口读取速度相对较慢,即使发生串行事件,时间耗尽TimeoutException也会抛出a.当串行事件发生时,我该怎么做才能停止未来的超时时钟?
我试了一下,AtomicReference但没有改变任何东西:
public class CommunicationTask implements Callable<Response>, Observer {
private AtomicReference atomicResponse = new AtomicReference(new Response("timeout"));
private CountDownLatch latch = new CountDownLatch(1);
private SerialPort port;
CommunicationTask(SerialCommunicator …Run Code Online (Sandbox Code Playgroud)