Executor Service - timeout of thread

后端 未结 3 1154
Happy的楠姐
Happy的楠姐 2021-02-04 19:56

While I was exploring ExecutorService, I encountered a method Future.get() which accepts the timeout.

The Java doc of this method

3条回答
  •  無奈伤痛
    2021-02-04 20:30

    The timeout on get() is for how long the 'client' will wait for the Future to complete. It does not have an impact on the future's execution.

    Object result;
    int seconds = 0;
    while ((result = fut.get.(1, TimeUnit.SECOND)) == null) {
        seconds++;
        System.out.println("Waited " + seconds + " seconds for future";
    }
    

提交回复
热议问题