如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的 checkAccess 方法就会被调用,这可能抛出 SecurityException。 如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。 如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException。 如果该线程在一个 Selector 中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的 wakeup 方法一样。 如果以前的条件都没有保存,则该线程的中断状态将被设置。 中断一个不处于活动状态的线程不需要任何作用。 抛出: SecurityException - 如果当前线程无法修改该线程
测试当前线程是否已经中断。线程的_中断状态_ 由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。 线程中断被忽略,因为在中断时不处于活动状态的线程将由此返回 false 的方法反映出来。 **返回:**如果当前线程已经中断,则返回 true;否则返回 false。
测试线程是否已经中断。线程的_中断状态_ 不受该方法的影响。 线程中断被忽略,因为在中断时不处于活动状态的线程将由此返回 false 的方法反映出来。 **返回:**如果该线程已经中断,则返回 true;否则返回 false。
static volatile boolean isStop = false;
private static void m1_volatile() {
new Thread(() -> {
while (true) {
if (isStop) {
System.out.println(Thread.currentThread().getName() + "\t isStop被修改为true,程序停止");
break;
}
System.out.println("t1 -----hello volatile");
}
}, "t1").start();
//暂停毫秒
try {
TimeUnit.MILLISECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
isStop = true;
}, "t2").start();
}
static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
private static void m2_atomicBoolean() {
new Thread(() -> {
while (true) {
if (atomicBoolean.get()) {
System.out.println(Thread.currentThread().getName() + "\t atomicBoolean被修改为true,程序停止");
break;
}
System.out.println("t1 -----hello atomicBoolean");
}
}, "t1").start();
//暂停毫秒
try {
TimeUnit.MILLISECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
atomicBoolean.set(true);
}, "t2").start();
}
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().getName() + "\t isInterrupted()被修改为true,程序停止");
break;
}
System.out.println("t1 -----hello interrupt api");
}
}, "t1");
t1.start();
System.out.println("-----t1的默认中断标志位:" + t1.isInterrupted());
//暂停毫秒
try {
TimeUnit.MILLISECONDS.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
//t2向t1发出协商,将t1的中断标志位设为true希望t1停下来
new Thread(() -> {
t1.interrupt();
}, "t2").start();
//t1.interrupt(); //main线程中断
核心思想:在需要中断的线程中不断监听中断状态,一旦发生中断,就执行相应的中断处理业务逻辑 stop 线程
public static void main(String[] args) {
//实例方法interrupt()仅仅是设置线程的中断状态位设置为true,不会停止线程
Thread t1 = new Thread(() -> {
for (int i = 1; i <= 300; i++) {
System.out.println("-----: " + i);
}
System.out.println("t1线程调用interrupt()后的的中断标识02:" + Thread.currentThread().isInterrupted());
}, "t1");
t1.start();
System.out.println("t1线程默认的中断标识:" + t1.isInterrupted());//false
//暂停毫秒
try {
TimeUnit.MILLISECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();//true
System.out.println("t1线程调用interrupt()后的的中断标识01:" + t1.isInterrupted());//true
try {
TimeUnit.MILLISECONDS.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1线程调用interrupt()后的的中断标识03:" + t1.isInterrupted());//????---false中断不活动的线程不会产生任何影响。
}
t1线程默认的中断标识:false //活跃的线程默认中断标志位为false
-----: 1
-----: 2
-----: 3
...
-----: 132
t1线程调用interrupt()后的的中断标识01:true //interrupt方法仅修改中断标志位
-----: 133
-----: 134
...
-----: 300
t1线程调用interrupt()后的的中断标识02:true //再次监听线程中断标志位
//对于一个不活跃的线程,调用interrupt方法不产生任何运行,可以理解为恢复中断标志位默认值
t1线程调用interrupt()后的的中断标识03:false
//线程结束
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().getName() + "\t " +
"中断标志位:" + Thread.currentThread().isInterrupted() + " 程序停止");
break;
}
System.out.println("-----hello InterruptDemo3");
}
}, "t1");
t1.start();
//暂停几秒钟线程
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> t1.interrupt(), "t2").start();
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();//为什么要在异常处,再调用一次??
e.printStackTrace();
}
public static void main(String[] args) {
//测试当前线程是否被中断(检查中断标志),返回一个boolean并清除中断状态,
// 第二次再调用时中断状态已经被清除,将返回一个false。
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
System.out.println("----1");
Thread.currentThread().interrupt();// 中断标志位设置为true
System.out.println("----2");
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
Thread.interrupted();//静态方法
Thread.currentThread().isInterrupted();//实例方法
}
private native boolean isInterrupted(boolean ClearInterrupted);
方法会根据传入的ClearInterrupted 参数确定是否重置中断标志位 private static void syncWaitNotify() {
Object objectLock = new Object();
new Thread(() -> {
synchronized (objectLock) {
System.out.println(Thread.currentThread().getName() + "\t ----come in");
try {
objectLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
}
}, "t1").start();
//暂停几秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
synchronized (objectLock) {
objectLock.notify();
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
}
}, "t2").start();
}
private static void syncWaitNotify() {
Object objectLock = new Object();
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (objectLock) {
System.out.println(Thread.currentThread().getName() + "\t ----come in");
try {
objectLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
}
}, "t1").start();
//暂停几秒钟线程
//try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
synchronized (objectLock) {
objectLock.notify();
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
}
}, "t2").start();
}
private static void lockAwaitSignal() {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(() -> {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "\t ----come in");
condition.await();
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, "t1").start();
//暂停几秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
lock.lock();
try {
condition.signal();
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
} finally {
lock.unlock();
}
}, "t2").start();
}
private static void lockAwaitSignal() {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "\t ----come in");
condition.await();
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, "t1").start();
//暂停几秒钟线程
//try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
lock.lock();
try {
condition.signal();
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
} finally {
lock.unlock();
}
}, "t2").start();
}
Thread t1 = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "\t ----come in" + System.currentTimeMillis());
LockSupport.park();
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒" + System.currentTimeMillis());
}, "t1");
t1.start();
//暂停几秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
LockSupport.unpark(t1);
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
}, "t2").start();