一、复习上一节内容
- wait()方法、中断正在运行的线程会抛出java.lang.InterruptedException、当线程调用共享对象的wait()方法时,当前线程只会释放当前共享变量的锁,不会释放该线程所持有的其他共享变量的锁。
- wait(long timeout,int nanos)实现、wait(0)内部调用了wait()方法、notify()随机唤醒、notifyAll()全部唤醒、join方法、sleep方法、yield方法,以及sleep与yield方法的区别
二、线程中断interrupt
- 使用interrupt()方法去中断线程,isInterrupted()来检测这个线程是否被中断了
package com.ruigege.threadFoundation1;
public class SubThreadInterruptedState {
public static void main(String[] args) throws InterruptedException{
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
System.out.println("子线程没有中断");
}
System.out.println("子线程虽然中断了,但是并没有结束");
}
});
thread1.start();
Thread.sleep(5);//保证子线程能够执行起来
thread1.interrupt();//中断子线程,可以理解为时间片这里不给它,一会再给它因此子线程会持续运行结束,这函数就是一个表姐而已
System.out.println(thread1.isI