1. interrupt knowledge points
The following summary is based on JDK8
This article will not fully explain interrupt, but will only list some more important points. For a complete understanding of Thread.interrupt, you can see the reference materials.
Here are some new articles that help understand references:
After the interrupt method is called, the interrupt flag is set to true for threads in the BLOCKED state. Whether to respond to interrupts (aware of changes in this flag bit) depends on the design of the API. JDK's blocking IO API, Synchronized synchronization block, and many methods in Lock (excluding lockInterruptibly) do not respond to interrupts. Of course, the calling thread can use flag bit judgment to make the API designed by itself respond to interrupts.
After the interrupt method is called, the interruptedException** will be thrown on the thread in the WAITING/TIMED_WAITING state and the interrupt flag bit false** will be set. For example, after the thread calls Thread.sleep, Object.wait().
If the thread has not started (NEW), or has ended (TERMINATED), calling interrupt() has no effect on it, and the interrupt flag will not be set.
Best practice: Sometimes some methods are not allowed to be interrupted or cancelled in design, but when other threads send interrupt requests, they also need to keep marks to facilitate other callers to "understand the situation".
public Task getNextTask(BlockingQueue<Task> queue) { boolean interrupted = false; try { while (true) { try { return queue.take(); } catch (InterruptedException e) { //The dependency status mark in fianlly interrupted = true; //fall through and retry } } } finally { if (interrupted) //Remark in fianlly to ensure that there is no missing interrupt notification Thread.currentThread().interrupt(); }}Some cancel operations can be implemented using interrupts. For example:
package concurrent;import java.util.concurrent.BlockingQueue;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Created by wanshao * Date: 2017/12/18 * Time: 3:42 pm **/public class InterruptExample { public static void main(String[] args) throws InterruptedException { InterruptTask interruptTask = new InterruptTask(); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(interruptTask); Thread.sleep(100); interruptTask.cancel(); executorService.shutdown(); }}/** * A task that responds to interrupt*/class InterruptTask implements Callable<Integer> { private BlockingQueue<Task> queue; //Save the thread to be interrupted Thread t; @Override public Integer call() throws InterruptedException { System.out.println("start a blocked task"); try { t = Thread.currentThread(); Thread.currentThread().sleep(50000); } catch (InterruptedException e) { System.out.println("be interrupted"); e.printStackTrace(); } return 0; } public void cancel() { System.out.println("cacel a task...."); //Calling Thread.currentThread() here will get the main thread, rather than the thread in the thread pool if (!t.isInterrupt()) { t.interrupt(); } }}Summarize
The above is the update in java based on JDK8 summary introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!