java中interrupt_Java中的Interrupt使用

初心

用interrupt中断程序

初步实现

public class InterruptionInJava implementsRunnable{

@Overridepublic voidrun() {while (true) {if(Thread.currentThread().isInterrupted()) {

System.out.println("Yes!! I'm Interupted, but I'm still running");

}else{

}

}//System.out.println("Oh, myGod!");

}public static voidmain(String[] args) {

Thread thread= new Thread(new InterruptionInJava(), "testThread");

thread.start();try{

Thread.sleep(1000);

}catch(InterruptedException e) {

}

System.out.println("Begin Interupt...");

thread.interrupt();

System.out.println("End Interupt...");

}

}

输出

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

Yes!! I'm Interupted, but I'm still running

.....

问题:虽然是被中断状态,但实际并未中断

interrupt说明

在java中主要有3个相关方法,interrupt(),isInterrupted()和interrupted()。

interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从,由具体的代码实现决定。

isInterrupted(),用来判断当前线程的中断状态(true or false)。

interrupted()是个Thread的static方法,用来恢复中断状态(!!!)

解决不中断问题

处于被中断状态时,return 或 bread 中断线程

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public class InterruptionInJava implementsRunnable{

@Overridepublic voidrun() {while (true) {if(Thread.currentThread().isInterrupted()) {

System.out.println("Yes!! I'm Interupted, but I'm still running");return;

}else{

}

}

}public static voidmain(String[] args) {

Thread thread= new Thread(new InterruptionInJava(), "testThread");

thread.start();try{

Thread.sleep(1000);

}catch(InterruptedException e) {

}

System.out.println("Begin Interupt...");

thread.interrupt();

System.out.println("End Interupt...");

}

}

View Code

等价开关

public class InterruptionInJava2 implementsRunnable{private volatile static boolean on = false;

@Overridepublic voidrun() {while (!on) {try{

System.out.println("begin Sleep");

Thread.sleep(10000000);

System.out.println("end Sleep");

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("Oh, myGod!");

}

}public static voidmain(String[] args) {

Thread thread= new Thread(new InterruptionInJava2(), "testThread");

thread.start();try{

System.out.println("main Begin sleep");

Thread.sleep(5000);

System.out.println("main End sleep");

}catch(InterruptedException e) {

}

InterruptionInJava2.on= true;

}

}

使用静态变量on

但是在run方法中Thread.sleep(10000000),😱何时结束?

开关中断解决方案

public class InterruptionInJava2 implementsRunnable{private volatile static boolean on = false;

@Overridepublic voidrun() {while (!on) {try{

System.out.println("begin Sleep");

Thread.sleep(10000000);

System.out.println("end Sleep");

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("Oh, myGod!");

}

}public static voidmain(String[] args) {

Thread thread= new Thread(new InterruptionInJava2(), "testThread");

thread.start();try{

System.out.println("main Begin sleep");

Thread.sleep(5000);

System.out.println("main End sleep");

}catch(InterruptedException e) {

}

InterruptionInJava2.on= true;

System.out.println("Begin Interupt...");

thread.interrupt();

System.out.println("End Interupt...");

}

}

输出

begin Sleep

main Begin sleep

main End sleep

Begin Interupt...

End Interupt...

java.lang.InterruptedException: sleep interrupted

at java.lang.Thread.sleep(Native Method)

at com.jihite.templet.JavaBase.InterruptionInJava2.run(InterruptionInJava2.java:11)

at java.lang.Thread.run(Thread.java:748)

Oh, myGod!

Process finished with exit code 0

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值