It is easy to start tasks and threads. However, sometimes we want to end a task or thread prematurely, perhaps because the user canceled the operation, or the application needs to be shut down quickly.
It's not easy to get tasks and threads to stop safely/quickly/reliably. Java does not provide any mechanism to safely terminate a thread, but it provides interrupts, a collaborative mechanism that enables one thread to terminate the work of another thread.
1, Mission canceled
If the external code can place it in the "done" state before an operation is completed properly, the operation can be called a cancellation. The reasons for canceling an operation are:
- User request Cancellation
- Operations with a time limit
- Application events, such as dividing the search task into multiple threads, as long as one is found, the other threads do not need to continue.
- Error
- Shut down
"Java Concurrency Programming Combat" (vi)----Cancel and close