1.使用interrupt关闭线程
/**
* 使用interrupt关闭线程,注意不可强制,而是要判断true如果当前线程已被中断; false除此以外。!Thread.currentThread().isInterrupted()
*/
public class RightWayStopThreadWithoutSleep implements Runnable{
@Override
public void run() {
int num = 0;
while (!Thread.currentThread().isInterrupted() && num<=Integer.MAX_VALUE /2){
if(num%10000==0){
System.out.println(num+"是1000的倍数");
}
num++;
}
System.out.println("结束啦");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
2.堵塞状态中断的方式是抛出异常:
1.循环里面使用了Thread.sleep(1000);代表每次运行都延迟1s,最下面的5s是整个线程运行5s然后会抛出异常结束线程
2.循环里面可以不用Thread.currentThread().isInterrupted()来判断线程是否要到点关闭了
/**
* 如果在执行过程中,每次循环都会调用sleep或wait等方法,那么不需要每次迭代都检查是否已中断
*/
public class RightWayStopThreadWithSleepEveryLoop {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = ()->{
int num = 0;
try {
while (num<=10000000){
if (num%10 ==0){
System.out.println(num+"是10的倍数");
}
num+=10;
Thread.sleep(1000);
System.out.println("hhh");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
3.把try-catch放在while里面堵塞状态中断会失效:
4.把try-catch放在while里面堵塞状态中断会失效怎么办?:1.传递中断
1.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢
/**
* 最佳时间 catch了InterruptedEcxetion之后的优先选择
* :在方法签名中抛出异常
* 那么在run()就会强制try/catch
*/
public class RightWayStopThreadInProd implements Runnable{
@Override
public void run() {
while (true && !Thread.currentThread().isInterrupted()){
System.out.println("我来啦");
new RightWayStopThreadInProd().throwInMethod();
}
}
private void throwInMethod(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd());
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
2.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢try-catch是在使用的地方try-catch的这样可以根据不同的需求做不同的处理
/**
* 最佳时间 catch了InterruptedEcxetion之后的优先选择
* :在方法签名中抛出异常
* 那么在run()就会强制try/catch
*/
@Slf4j
public class RightWayStopThreadInProd implements Runnable{
@Override
public void run() {
while (true && !Thread.currentThread().isInterrupted()){
System.out.println("我来啦");
try {
new RightWayStopThreadInProd().throwInMethod();
} catch (InterruptedException e) {
log.error("我在这中断了,我要看我跳出来了之后干嘛",e.getMessage());
break;
}
}
}
private void throwInMethod() throws InterruptedException {
Thread.sleep(2000);
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd());
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
5.把try-catch放在while里面堵塞状态中断会失效怎么办?:2.恢复中断
既然是因为每次catch之后会把interrupt给清掉,导致无法获取是否中断,那么在catch中:Thread.currentThread().interrupt();实现中断就可以了