What does Future.cancel() do if not interrupting?

后端 未结 3 1376
甜味超标
甜味超标 2021-02-12 13:54

From java docs on Future.cancel()

boolean cancel(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task. This

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 14:27

    My question is what does cancel do if mayInterruptIfRunning is false? how does it cancel or stop the task from executing if it has already been run?

    If the task has already started running, and mayInterruptIfRunning is false, then there is nothing to be done. In Java, interrupting a thread is considered to be the only safe way of stopping it short of completion - and even that requires the task to "comply" by checking for interruption at some implementation-specific interval.

    Related: How do you kill a thread in Java?

提交回复
热议问题