Use status variables to end the taskSometimes we can use a state variable (such as a Boolean value) to end task execution. This method is very peaceful and provides you with the opportunity to perform some operations before the task is terminated. For example:
public class StateStopTask implements Runnable{ private static volatile boolean isCancled = false; public void run() { while(true) { if(isCancled == true) { System.out.println("StateStopTask is stop"); return; } } } public static void main(String []args) { new Thread(new StateStopThread()).start(); Thread.sleep(3000); isCancled = true; }}You can also directly use thread. Interrupt () to set the thread interruption identifier and check the identifier in the program to avoid the definition of state variables.
public class StateStopTask implements Runnable{ public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("StateStopTask is running"); } System.out.println("StateStopTask is stopping"); } public static void main(String []args) { Thread t = new Thread(new StateStopThread()).start(); Thread.sleep(3000); t.interrupt(); }}
End at blockingThe preceding state variables can only be terminated when the detection is performed online. Sometimes, we need to terminate the thread at any time and place, especially when it is blocked. A task is blocked for the following reasons:
- By calling sleep (milliseconds), the task enters the sleep state. In this case, the task does not run at the specified time.
- You can use wait () to suspend a thread. Until the thread gets the notify () or yyall () message (or the java. util. the thread enters the ready state only when the concurrent class library has a medium-price signal () or signalall () message.
- The task is waiting for an input/output to complete.
- The task tries to call its synchronization control method on an object, but the object lock is unavailable because another task has obtained the lock.
InterruptedIf the program is blocked due to sleep or wait (), if the interrupt () method is called, an interruptedexcetion exception is thrown and the interruption status is cleared. When the program calls the interrupt () method due to Io and lock blocking, it does not throw interruptedexception, but sets the interruption status. Interrupted () can be used to detect the interruption status and clear the interruption status if the interruption is in progress. Therefore, in the program, the following methods are generally used to detect the interruption and handle the problem:
public class InterruptedTask implements Runnable{ public void run() { while(!Thread.currentThread().interrupted()) { try { System.out.println("InterruptedTask running"); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("interrupted by InterruptedException ") } } System.out.println("InterruptedTask Interrupted") }}In modern Java code, it is not recommended to operate thread objects directly. Instead, execute all operations through executor as much as possible. If you call shutdownnow () on executor, it will send an interrupt () call to all the threads it starts. This is common because an executor usually executes the same class of tasks, so it is often expected to close all tasks of the executor. Then, you sometimes want to interrupt a single task. If executor is used, the context of the task can be held by calling submit () instead of executor (), and submit () returns a generic future <?>, There is an unmodified parameter. Because you cannot call get (), you can use this futrue object to call cancle (). If you pass true to cancle (), the interrupt () method will be called in this thread. Lockinterruptibly () is a lockable blocking operation ().
Concurrent Programming (4): end a task