本文主要总结在java中停止线程的方法

在java中有以下三种方法可以终止正在运行的线程:

1、使用退出标志

2、使用stop方法强行终止线程,但是不推荐,因为stop和suspend、resume一样都是过时的方法

3、使用interrup方法中断线程

停止不了的线程

本例将使用interrupt方法来停止进程,看看效果:

public class MyThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 1234; i++) {
System.out.println("i=" + (i + 1));
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(2000);
thread.interrupt(); } catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
}
}

可以看出,线程没有停止

判断线程是否是停止状态

在java的SDK中,Thread.java类里提供了两种方法:

(1)this.interrupted():测试当前线程是否已经中断

(2)this.isInterrupted():测试线程是否已经中断

首先看一下interrupted()

实战验证一下:

public class MyThread extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 10000; i++) {
System.out.println("i=" + (i + 1));
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
System.out.println("是否停止1?="+thread.interrupted());
System.out.println("是否停止2?="+thread.interrupted()); } catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}

运行结果如下:

.......................

i=9998
i=9999
i=10000
是否停止1?=false
是否停止2?=false
end!

由于interrupted是测试当前线程,当前线程为main,一直没有中断,所以返回两个false

将代码修改一下:

public class Run {
public static void main(String[] args) {
Thread.currentThread().interrupt();
System.out.println("是否停止1?=" + Thread.interrupted());
System.out.println("是否停止2?=" + Thread.interrupted());
System.out.println("end!");
}
}

返回如下信息:

是否停止1?=true
是否停止2?=false
end!

由于interrupted方法有清除状态的功能,所以第二次返回false

再看看isInterrupted()方法

public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
System.out.println("是否停止1?=" + thread.isInterrupted());
System.out.println("是否停止2?=" + thread.isInterrupted()); } catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}

运行结果:

i=123367
i=123368
是否停止1?=true
是否停止2?=true
i=123369
i=123370
i=123371
i=123372

总结:

(1)this.interrupted():测试当前线程是否已经中断,执行后具有将状态标志清除为false的功能

(2)this.isInterrupted():测试线程是否已经中断,但不清除状态标志

能停止的线程--异常法

可以在线程中用for语句来判断一下线程是否是停止状态,如果是停止状态,则后面的代码不再运行即可

public class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 500000; i++) {
if (this.interrupted()) {
System.out.println("已经是停止状态了,我要退出了!");
throw new InterruptedException();
}
System.out.println("i=" + (i + 1));
}
System.out.println("我在for下面");
} catch (InterruptedException e) {
System.out.println("进MyThread.java类run方法中的catch了!");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(2000);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}

在sleep中停止

如果线程在sleep状态下停止线程,有什么效果呢?

public class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
System.out.println("run begin");
Thread.sleep(200000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("在沉睡中被停止!进入catch!" + this.isInterrupted());
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(200);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}

运行结果:

run begin
end!
在沉睡中被停止!进入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wuyudong.test1.MyThread.run(MyThread.java:9)

结果说明如果在sleep状态下停止线程,会进入catch语句,并且清除停止状态值,使之变成false

下面进行相反的操作,修改一下:

public class MyThread extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 100000; i++) {
System.out.println("i=" + (i + 1));
}
System.out.println("run begin");
Thread.sleep(200000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("先停止,再遇到了sleep!进入catch!");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
thread.interrupt();
System.out.println("end!");
}
}

运行结果如下:

i=100000
run begin
先停止,再遇到了sleep!进入catch!
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wuyudong.test1.MyThread.run(MyThread.java:12)

能停止的线程--暴力停止

使用stop方法停止线程是十分暴力的

public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
super.run();
try {
while (true) {
i++;
System.out.println("i=" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("先停止,再遇到了sleep!进入catch!");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(8000);
thread.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}

运行结果如下:

i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8

方法stop与异常

调用stop方法的时候会抛出java.lang.ThreadDeath异常,但是通常此异常不需要显式地捕捉

public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
super.run();
try {
while (true) {
i++;
System.out.println("i=" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("先停止,再遇到了sleep!进入catch!");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(8000);
thread.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}

使用return停止线程

将方法interrupt与return结合使用也能实现停止线程的效果

public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
super.run();
while(true){
if(this.isInterrupted()){
System.out.println("停止了!");
return;
}
System.out.println("timer="+System.currentTimeMillis());
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException{
try {
MyThread thread = new MyThread();
thread.start();
Thread.sleep(2000);
thread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}

运行结果如下:

timer=1452841019023
timer=1452841019023
timer=1452841019023
timer=1452841019023
停止了!

java多线程系列7-停止线程的更多相关文章

  1. java多线程系列六、线程池

    一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池. 2. 使用线程池的好处 a) 降低资源的消耗.使用线程池不用频繁的创建线程和销毁线程 b) 提高响应速度,任 ...

  2. (Java多线程系列二)线程间同步

    Java多线程间同步 1.什么是线程安全 通过一个案例了解线程安全 案例:需求现在有100张火车票,有两个窗口同时抢火车票,请使用多线程模拟抢票效果. 先来看一个线程不安全的例子 class Sell ...

  3. (Java多线程系列三)线程间通讯

    Java多线程间通讯 多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同. 1.使用wait()和notify()方法在线程中通讯 需求:第一个线程写入(input)用户,另一个线程 ...

  4. (Java多线程系列九)线程池

    线程池 1.什么是线程池 线程池是指在初始化一个多线程应用程序过程中创建一个线程集合,然后在需要执行新的任务时重用这些线程而不是新建一个线程.线程池中线程的数量通常取决于可用内存数量和应用程序的需求. ...

  5. Java多线程系列四——控制线程执行顺序

    假设有线程1/线程2/线程3,线程3必须在线程1/线程2执行完成之后开始执行,有两种方式可实现 Thread类的join方法:使宿主线程阻塞指定时间或者直到寄生线程执行完毕 CountDownLatc ...

  6. Java多线程系列三——实现线程同步的方法

    两种实现线程同步的方法 方法 特性 synchronized 不需要显式地加解锁,易实现 ReentrantLock 需要显式地加解锁,灵活性更好,性能更优秀,结合Condition可实现多种条件锁 ...

  7. java多线程系列8-线程的优先级

    在java中设置线程优先级使用setPriority,在jdk中的源代码如下: public final void setPriority(int newPriority) { ThreadGroup ...

  8. Java多线程系列十——BlockingQueue

    参考资料:http://ifeve.com/java-synchronousqueue/http://www.cnblogs.com/jackyuj/archive/2010/11/24/188655 ...

  9. Java多线程系列--“JUC线程池”01之 线程池架构

    概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...

随机推荐

  1. 【cs229-Lecture15】奇异值分解

    PCA的实现一般有两种,一种是用特征值分解去实现的,一种是用奇异值分解去实现的. 内容: PCA  (主成份分析)是一种直接的降维方法,通过求解特征值与特征向量,并选取特征值较大的一些特征向量来达到降 ...

  2. mysql ODBC connector相关问题

    mysql ODBC connector我安装了,怎么就不成功了 进到命令行,运行下边的:C:\>cd \windows\SysWOW64 C:\Windows\SysWOW64>odbc ...

  3. 一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL

    介绍 不知道大家在使用 ASP.NET MVC 时有没有一些扩展要求,反正我是有很多.在使用 MVC 这几年(PS:我是从 1.0 开始学,2.0.3.0 开发至今),我深深地觉得 MVC 的扩展性真 ...

  4. Flex 远程加载crossdomain.xml 解决

    局域网部署Flex项目的时候加载不出来,分析了一下http发现在请求连接“http://fpdownload.adobe.com/pub/swz/crossdomain.xml”,这里出了问题,跨域的 ...

  5. 个人对joomla3.2x和joomla2.5X浅薄看法

    很久没有写joomla文章了,发现想写的东西还是挺多的,后面抽时间补回来,其实更多还是php的一些东西.joomla3.0以后系统改变挺大,后台都是用bootstrap作为主题,个人对这个无爱,因为他 ...

  6. STL中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...

  7. [转载]SharePoint 2013搜索学习笔记之搜索构架简单概述

    Sharepoint搜索引擎主要由6种组件构成,他们分别是爬网组件,内容处理组件,分析处理组件,索引组件,查询处理组件,搜索管理组件.可以将这6种组件分别部署到Sharepoint场内的多个服务器上, ...

  8. [linux]收集一些好玩的命令

    1.rev命令 反转输出,输入的字符串. 在终端中输入:rev 输入需要字符串(支持中文) 2.asciiview命令 安装aview:apt-get install aview 再安装imagema ...

  9. Laravel 5 事件的使用

    事件类通常被保存在 app/Events 目录下,而它们的处理程序则被保存在 app/Handlers/Events 目录下. 事件的创建 下面我们用artisan来创建一个事件,比如叫CqhTest ...

  10. 我理解的Android加载器

    Android的加载器(loader)是从Android 3.0开始出来的东西.要理解这里需要先理解为什么会出现加载器(也有地方把它说成是装载器)呢? 如果没有加载器... 首先Activity是我们 ...