方法 | 说明 |
public void interrupt() | 实例方法 interrupt() 仅仅是设置线程的中断状态为true, 发起一个协商而不会立刻停止线程 |
public static boolean interrupted() | 静态方法 返回当前线程的中断状态,测试当前线程是否已被中断 将当前线程的中断状态清零并重新设为 false,清除线程的中断状态 |
public boolean isInterrupted() | 实例方法 判断当前线程是否呗中断(通过检查中断标志位) |
如何中断运行中的线程:
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class InterruptDemo {
static volatile boolean isStop = false;
static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public static void main(String[] args) {
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 Interrupted");
}
}, "t1");
t1.start();
try{ TimeUnit.MILLISECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> {
t1.interrupt();
},"t2").start();
}
public static void m2() {
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();
}
public static void m1() {
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();
}
}
当前线程的中断标识为 true, 是不是线程就立刻停止:
import java.util.concurrent.TimeUnit;
public class InterruptDemo2 {
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;
}
try{
Thread.sleep(200);
} catch (InterruptedException e) {
// 如果不中断,则程序会进入死循环
// Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println("hello InterruptDemo2");
}
}, "t1");
t1.start();
try{ TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(()-> t1.interrupt(),"t2").start();
}
}
静态方法 Thread.interrupted() 理解:
public class InterrupteDemo4 {
public static void main(String[] args) {
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();
System.out.println("-----2");
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());
}
}
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
public boolean isInterrupted() {
return isInterrupted(false);
}
private native boolean isInterrupted(boolean ClearInterrupted);
import java.util.concurrent.TimeUnit;
public class LockSupportDemo {
public static void main(String[] args) {
Object objectLock = new Object();
// 必须放在同步块或者方法里
// 将 notify 放在 wait 方法前面,程序无法执行,无法唤醒
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();
}
}
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockSupportDemo {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
// 必须放在同步块或者方法里
// 将 signal 放在 await 方法前面,程序无法执行,无法唤醒
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();
condition.signal();
System.out.println(Thread.currentThread().getName() + "\t ----发出通知");
lock.unlock();
},"t2").start();
}
}
public class LockSupportDemo {
/**
* Permit 许可证默认没有不能放行,所以一开始调 park() 方法当前线程就会阻塞,直到别的线程给当前线程的发放 permit, park 方法还会被唤醒
* 调用 uppark(thread) 方法后,就会将 thread 线程的许可证 permit 发放,会自动唤醒 park 线程,即之前阻塞中的 LockSupport.park() 方法会立即返回
* @param args
*/
public static void main(String[] args) {
// 无锁块要求
// 加锁和解锁不需要顺序
Thread t1 = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "\t ----come in");
LockSupport.park();
System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
}, "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();
}
}