Java中如何正确而优雅的终止运行中的线程
2016-09-09 10:42
706 查看
Java中终止线程的方式主要有三种:
1、使用stop()方法,已被弃用。原因是:stop()是立即终止,会导致一些数据被到处理一部分就会被终止,而用户并不知道哪些数据被处理,哪些没有被处理,产生了不完整的“残疾”数据,不符合完整性,所以被废弃。So, forget it!
2、使用volatile标志位
看一个简单的例子:
首先,实现一个Runnable接口,在其中定义volatile标志位,在run()方法中使用标志位控制程序运行。
public class MyRunnable implements Runnable {
//定义退出标志,true会一直执行,false会退出循环
//使用volatile目的是保证可见性,一处修改了标志,处处都要去主存读取新的值,而不是使用缓存
public volatile boolean flag = true;
public void run() {
System.out.println("第" + Thread.currentThread().getName() + "个线程创建");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//退出标志生效位置
while (flag) {
}
System.out.println("第" + Thread.currentThread().getName() + "个线程终止");
}
}然后,在main()方法中创建线程,在合适的时候,修改标志位,终止运行中的线程。
public class TreadTest {
public static void main(String[] arg) throws InterruptedException {
MyRunnable runnable = new MyRunnable();
//创建3个线程
for (int i = 1; i <= 3; i++) {
Thread thread = new Thread(runnable, i + "");
thread.start();
}
//线程休眠
Thread.sleep(2000L);
System.out.println("——————————————————————————");
//修改退出标志,使线程终止
runnable.flag = false;
}
}最后,运行结果,如下:
![](https://img-blog.csdn.net/20160909100327229?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3、使用interrupt()中断的方式,注意使用interrupt()方法中断正在运行中的线程只会修改中断状态位,可以通过isInterrupted()判断。如果使用interrupt()方法中断阻塞中的线程,那么就会抛出InterruptedException异常,可以通过catch捕获异常,然后进行处理后终止线程。有些情况,我们不能判断线程的状态,所以使用interrupt()方法时一定要慎重考虑。
看一个简单的例子(中断运行中的线程):
public class MyRunnable2 implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName() + " 线程创建");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//运行时的线程被中断后,只会修改中断标记,不会抛出异常
while(Thread.currentThread().isInterrupted()){
}
System.out.println(Thread.currentThread().getName() + " 线程被中断");
}
}
1、使用stop()方法,已被弃用。原因是:stop()是立即终止,会导致一些数据被到处理一部分就会被终止,而用户并不知道哪些数据被处理,哪些没有被处理,产生了不完整的“残疾”数据,不符合完整性,所以被废弃。So, forget it!
2、使用volatile标志位
看一个简单的例子:
首先,实现一个Runnable接口,在其中定义volatile标志位,在run()方法中使用标志位控制程序运行。
public class MyRunnable implements Runnable {
//定义退出标志,true会一直执行,false会退出循环
//使用volatile目的是保证可见性,一处修改了标志,处处都要去主存读取新的值,而不是使用缓存
public volatile boolean flag = true;
public void run() {
System.out.println("第" + Thread.currentThread().getName() + "个线程创建");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//退出标志生效位置
while (flag) {
}
System.out.println("第" + Thread.currentThread().getName() + "个线程终止");
}
}然后,在main()方法中创建线程,在合适的时候,修改标志位,终止运行中的线程。
public class TreadTest {
public static void main(String[] arg) throws InterruptedException {
MyRunnable runnable = new MyRunnable();
//创建3个线程
for (int i = 1; i <= 3; i++) {
Thread thread = new Thread(runnable, i + "");
thread.start();
}
//线程休眠
Thread.sleep(2000L);
System.out.println("——————————————————————————");
//修改退出标志,使线程终止
runnable.flag = false;
}
}最后,运行结果,如下:
3、使用interrupt()中断的方式,注意使用interrupt()方法中断正在运行中的线程只会修改中断状态位,可以通过isInterrupted()判断。如果使用interrupt()方法中断阻塞中的线程,那么就会抛出InterruptedException异常,可以通过catch捕获异常,然后进行处理后终止线程。有些情况,我们不能判断线程的状态,所以使用interrupt()方法时一定要慎重考虑。
看一个简单的例子(中断运行中的线程):
public class MyRunnable2 implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName() + " 线程创建");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
//运行时的线程被中断后,只会修改中断标记,不会抛出异常
while(Thread.currentThread().isInterrupted()){
}
System.out.println(Thread.currentThread().getName() + " 线程被中断");
}
}
public class TreadTest { public static void main(String[] arg) throws InterruptedException { Runnable runnable = new MyRunnable2(); //创建3个线程 Thread thread1 = new Thread(runnable, "1"); Thread thread2 = new Thread(runnable, "2"); Thread thread3 = new Thread(runnable, "3"); thread1.start(); thread2.start(); thread3.start(); //线程休眠 Thread.sleep(2000L); //修改退出标志,使线程终止 thread1.interrupt(); thread2.interrupt(); thread3.interrupt(); } }执行结果:
相关文章推荐
- Java中如何优雅正确的终止线程
- Java中如何优雅正确的终止线程
- Java中如何正确的终止线程
- Java中如何正确的终止一个线程
- Java如何优雅的终止线程
- 如何终止线程的运行(C/C++)
- java程序中,如何安全的结束一个正在运行的线程?
- 如何创建并运行java线程
- 如何终止线程的运行(C/C++)
- 如何正确停止java中的线程
- 如何正确的终止正在运行的子线程
- java线程技术2_线程的创建运行终止
- 如何正确的终止正在运行的子线程
- Java 优雅的终止线程
- 如何终止线程的运行(C/C++)
- 解析C/C++中如何终止线程的运行
- java中,如何安全的结束一个正在运行的线程?
- java线程技术2_线程的创建运行终止
- 在java中如何防止从其它线程类运行run方法
- java如何实现多个线程并发运行