总工有三种方式可停止当前正在运行的线程:
使用退出标志,使线程正常退出,也就是当run方法完成后线程终止; 使用interrupt方法中断线程 ; 使用stop方法强行终止(不推荐使用该方法,目前该方法已作废)。
(1)使用退出标志,使线程正常退出,也就是当run方法完成后线程终止
代码示例:
public class MyThread extends Thread {
volatile boolean flag = false ; // 线程执行的退出标记
@Override
public void run() {
while(!flag) {
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠6秒
Thread.sleep(6000);
// 更改标记为true
t1.flag = true ;
}
}
输出效果:
MyThread...run...
MyThread...run...
(2)使用interrupt方法中断线程。
代码示例:
package com.company.test;
public class MyThread extends Thread {
// 线程执行的退出标记
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().isInterrupted());
System.out.println("MyThread...run...");
}
System.out.println(Thread.currentThread().isInterrupted());
}
public static void main(String[] args) throws InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread();
t1.start();
// 主线程休眠2秒
Thread.sleep(3000);
// 调用interrupt方法
t1.interrupt();//并不能直接中断对应子线程;之线程无法感知;
}
}
输出效果:
MyThread...run...
false
.....................
false
MyThread...run...
false
MyThread...run...
true
注意!!!
Thread.currentThread().isInterrupted()
显示结果为false;线程将一直运行。
示例代码:
public class MyThread extends Thread {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().isInterrupted());
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().isInterrupted());
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠2秒
Thread.sleep(3000);
// 调用interrupt方法,对子线程进行中断
t1.interrupt();
}
}
显示结果:
false
MyThread...run...
false
MyThread...run...
false
MyThread...run...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.company.test.MyThread.run(MyThread.java:10)
false
MyThread...run...
false
MyThread...run...
................
可见,在子线程中使用异常捕获之后,线程将无法感知上层方法对它的中断。
但非要让子线程感知中断,只能通过手动调用的方式:
修改后的代码:
public class MyThread extends Thread {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().isInterrupted());
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
/**
* 手动调用恢复中断
*/
Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
}
}
System.out.println(Thread.currentThread().isInterrupted());
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠2秒
Thread.sleep(3000);
// 调用interrupt方法,对子线程进行中断
t1.interrupt();
}
}
显示结果:
false
MyThread...run...
false
MyThread...run...
true
true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.company.test.MyThread.run(MyThread.java:10)Process finished with exit code 0
可见,通过手动调用后,线程执行可以被中断。
(3)使用stop方法强行终止
public class MyThread extends Thread {
volatile boolean flag = false ; // 线程执行的退出标记
@Override
public void run() {
while(!flag) {
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠2秒
Thread.sleep(6000);
// 调用stop方法
t1.stop();
}
}
现在编码中,通过stop强行停止线程的方法已经被废弃。