中断线程有两个方法:
方法一:
使用Thread实例的interrupt()方法。
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread();
t.start();
Thread.sleep(1000);
t.interrupt(); // 中断t线程
t.join(); // 等待t线程结束
System.out.println("end");
}
}
class MyThread extends Thread {
public void run() {
Thread hello = new HelloThread();
hello.start(); // 启动hello线程
try {
hello.join(); // 等待hello线程结束
} catch (InterruptedException e) {
System.out.println("interrupted!");
}
hello.interrupt();
}
}
class HelloThread extends Thread {
public void run() {
int n = 0;
while (!isInterrupted()) {
n++;
System.out.println(n + " hello!");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
}
上述实例解释:
- mian主线程中创建t线程,并运行,t线程中创建hello线程并运行。
- main主线程中等待1000毫秒执行中断t线程,以及等待t线程结束执行。
- t线程内捕获到异常,输出interrupted!。再执行中断hello线程。且当前t线程已经结束。
- hello线程run方法捕获到异常,停止执行。hello线程已经结束。
- main主线程收到t线程结束的消息,执行最后输出语句,main线程也结束。
方法二:设置标志位。
在线程中我们通常会使用一个线程共享变量running(标志位)来标识线程是否应该继续运行。
public class Main {
public static void main(String[] args) throws InterruptedException {
HelloThread t = new HelloThread();
t.start();
Thread.sleep(1);
t.running = false; // 标志位置为false
}
}
class HelloThread extends Thread {
public volatile boolean running = true;
public void run() {
int n = 0;
while (running) {
n ++;
System.out.println(n + " hello!");
}
System.out.println("end!");
}
}
注意到,在线程外部将running设置为false,t线程就结束。
这是根据线程中共享变量来判断,共享变量需要用到关键字volatile来声明。
volatile关键字的目的是告诉虚拟机:
- 每次访问变量时,总是获取主内存的最新值;
- 每次修改变量后,立刻回写到主内存。