即使在未来取消后,杰迪斯行动仍在继续

时间:2019-04-25 06:42:07

标签: java multithreading interrupt jedis

我正在通过未来做未来的Jedis手术(hgetall)。当调用get方法时未来超时时,我取消了未来。 Redis操作仍然继续发生。它不会被中断。

Future<Map<String,String>> future = executorService.submit(() -> {
    long startTime = Instant.now.toEpochMilli();
    Map cachedata = jedis.hgetall(primarykey);
    log.info("Total time for hgetall operation {}", Instant.now.toEpochMilli() - startTime);
});
try {
    future.get(2000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e ) {
    log.warn("jedis operation timeout");
    //set interrupt flag to true
    future.cancel(true);
} //other checked exception handling 

当Redis集群由于高负载而变慢时,它开始需要更长的时间来处理请求。发生这种情况时,以上future会在2秒内超时。使用cancel时,线程的中断标志应设置为true。之后,我希望jedis操作停止并遵守中断标志。但是该任务继续进行,并最终显示一条日志行“ hgetall操作的总时间为6000”。这表明在设置了中断标志后,该任务在后台运行了4秒钟。

我在集群模式下使用jedis并从连接池获取连接。 Java版本是8。

0 个答案:

没有答案
相关问题