< > Chapter 7: cancellation and closure

Java does not provide any mechanism to safely terminate threads, although thread Methods such as stop and suspend provide such a mechanism, but they have serious defects and should be avoided. However, Java provides an interrupt interruption mechanism, which is a collaboration mechanism that enables one thread to terminate the current work of another thread.

This collaboration is necessary. We rarely want a task thread or service to stop immediately, because this immediate stop will cause a shared data structure to be in an inconsistent state. Instead, a collaborative approach can be used when writing tasks and services: when they need to stop, they will first clear the work currently executing and then end.

7.1 task cancellation

If the external code can put an operation in the completion state before it completes normally, the operation can be called cancelable

One of the collaboration mechanisms is to set a cancellation requested flag, which is regularly viewed by the task.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>