CancellationException when using ExecutorService

后端 未结 1 1061
滥情空心
滥情空心 2021-02-20 13:50

I want to wait two tasks to finish then return the result of them but sometimes I get this error. Why? Where did CancellationException come from?

public class Sh         


        
1条回答
  •  深忆病人
    2021-02-20 14:08

    You ask your executor service to execute your callables with a timeout of 5 seconds. According to the javadoc:

    tasks that have not completed [by the end of the timeout] are cancelled

    My guess is that future.get() throws a CancellationException because the timeout has been reached and the executor calls future.cancel().

    You could either:

    • increase the timeout
    • catch an InterruptedException in your callable to handle the cancellation gracefully

    0 讨论(0)
提交回复
热议问题