线程的中断方法
第一种方法
- 使用 自定义标识符 来控制线程中断
public class ThreadDemo13 {
//1.使用 自定义标识符 来控制控制线程中断
private static boolean flag=false;
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
while (!flag){
System.out.println("别烦我");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("交易终止");
});
t1.start();
Thread.sleep(2000);
System.out.println("有内鬼终止交易");
flag=true;
}
}
第二种方法
- 通过调用interrupt()方法
- 1、直接调用interrupt()方法
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
//直接调用interrupt
while (!Thread.interrupted()){
System.out.println("别烦我");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//加入break才是终止执行
break;
}
}
System.out.println("转账终止");
});
t1.start();
Thread.sleep(2000);
System.out.println("有内鬼终止交易");
//让t1终止 使用此方法
t1.interrupt();
}
}
- 2、拿到当前线程在调用interrupt()方法(Thread.currentThread().isInterrupted())
public class ThreadDemo14 {
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
//拿到当前线程 判断是否终止
while (!Thread.currentThread().isInterrupted()){
System.out.println("别烦我");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//加入break才是终止执行
break;
}
}
System.out.println("转账终止");
});
t1.start();
Thread.sleep(2000);
System.out.println("有内鬼终止交易");
//让t1终止 使用此方法
t1.interrupt();
}
}
注:当调用interrupt方法时,一定要加上break终止代码否则只会出现暂时终止。
调用interrupt()方法的两种方法的区别
- Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志,也就是复位
public class ThreadDemo16 {
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
while (!Thread.interrupted()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//获取当前线程
Thread curr=Thread.currentThread();
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("---------------------");
Thread.interrupted();
//Thread.currentThread().interrupt();
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("打印当前线程状态"+curr.isInterrupted());
}
}
});
t1.start();
Thread.sleep(1000);
t1.interrupt();
}
}
打印结果
- Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志,不会复位
public class ThreadDemo16 {
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
while (!Thread.interrupted()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//获取当前线程
Thread curr=Thread.currentThread();
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("---------------------");
// Thread.interrupted();
Thread.currentThread().interrupt();
System.out.println("打印当前线程状态"+curr.isInterrupted());
System.out.println("打印当前线程状态"+curr.isInterrupted());
}
}
});
t1.start();
Thread.sleep(1000);
t1.interrupt();
}
}
打印结果
补:线程中的重要属性
public class ThreadDemo11 {
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
for (int i=0;i<10;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
//重要属性
System.out.println("线程的ID:"+t1.getId());
System.out.println("线程的名称:"+t1.getName());
System.out.println("线程的优先级:"+t1.getPriority());
System.out.println("线程的状态:"+t1.getState());
System.out.println("线程的类型:"+t1.isDaemon());
System.out.println("线程是否存活:"+t1.isAlive());
t1.start();