- sleep wait join 的线程如果被打断,会直接清除打断标记,将打断标记设置为false
public class WaitNotifyInterupt {static Object obj = new Object();public static void main(String[] args) {Thread threadA = new Thread(() -> {synchronized (obj) {try {System.out.println("begin");obj.wait();System.out.println("end");} catch (InterruptedException e) {e.printStackTrace();}}});threadA.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}threadA.interrupt();System.out.println("Thread end");}}
为什么sleep的线程被打断之后会清除打断标记?
正常线程调用interrupt()不能被打断 给你一个打断标记你自己根据标记去判断
如果是sleep则会响应打断所以会清除打断标记;
import java.util.concurrent.TimeUnit;class Lock3 {public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {System.out.println("t1------");try {TimeUnit.SECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}, "t1");t1.start();//主要为了让子线程 t1先运行TimeUnit.SECONDS.sleep(1);//本来你这里做了对t1的打断操作 为什么sleep要清除t1.interrupt();//falseSystem.out.println("t1的打断标记" + t1.isInterrupted());}
