网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > java语言

java中如何停止线程

栏目: java语言 / 发布于: / 人气:8.92K

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念。想知道java中如何停止线程?下面就一起来了解看看吧!

java中如何停止线程

一般来说线程执行完run()之后就自动结束了,不过有些时候我们需要线程不停的做一些事情,也就是使用while循环,那么这时候该如何停止线程呢?

这个问题需要分情况来讨论,如果线程做的事情不是耗时的,那么只需要使用一个标志即可,具体的代码如下:

class MyThread extends Thread {

private volatile boolean isStop = false;

public void run() {

while (!isStop) {

tln("do something");

}

}

public void setStop() {

isStop = true;

}

}

如果需要退出时,调用setStop()即可。这里使用了一个Java关键字volatile,这个关键字的目的是使isStop同步,也就是说在同一时刻只能由一个线程来修改isStop的值。

如果线程做的'事情是耗时或者说阻塞的(如调用了sleep,同步锁的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,调用该函数时会抛出InterruptedException异常,代码中通过捕获该异常,然后break出循环,就可以了。代码如下:

class MyThread extends Thread {

private volatile boolean isStop = false;

public void run() {

while (!isStop) {

try {

tln("do something");

p(1000);

} catch (InterruptedException e) {

tStackTrace();

break;//如果没有这一句,而且不调用setStop(),线程并不会结束,需要特别注意一下

}

}

}

public void setStop() {

isStop = true;

}

}

public class Tes1 {

public static void main(String[] args) {

MyThread t1 = new MyThread();

t();

try {

p(10);

top();

rrupt();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

tStackTrace();

}

}

}

这里既然说到了interrupt(),那么就不得不提另外两个函数:interrupted()和isInterrupted。看下这两个函数的作用:

public static boolean interrupted()

测试当前线程是否已经中断,并且清除线程的中断状态。

public boolean isInterrupted()

测试线程是否已经中断,线程的中断状态不受该方法的影响。

这两个函数的返回值含义:

如果当前线程已经中断,则返回 true;否则返回 false。

其实当线程阻塞并且调用了interrupt()时,不止是抛出InterruptedException异常还会调用interrupted()来清除线程的中断状态,所以在catch里面调用isInterrupted()会返回false。

为了更好的理解这两个函数,我们看个例子:

class MyThread extends Thread {

private int counter = 0;

public void run() {

boolean done = false;

try{

p(10);

}catch(InterruptedException ie){

tStackTrace();

return;

}

while (counter < 2000 &&!done) {

tln(counter++ + " in thread isInterrupted() "+isInterrupted());

if(isInterrupted()==true){

try{

tln("in thread after interrupted() "+isInterrupted());

sleep(100);

break;

}catch(InterruptedException ie){

tStackTrace();

break;

}

}

}

}

}

public class Tes1 {

public static void main(String[] args) {

final MyThread t1 = new MyThread();

t();

new Timer(true)dule(new TimerTask() {

public void run() {

tln("exec interrupt");

rrupt();

tln("in timer isInterrupted() "+terrupted());

}

}, 20);

}

}

执行效果如下(每一次执行打印的都不一样,因为没有做线程同步):

...

1300 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

1301 in thread isInterrupted() true

in thread after interrupted() true

rruptedException: sleep interrupted

at p(Native Method)

at ()

还可能是这样:

...

1568 in thread isInterrupted() false

exec interrupt

in thread after interrupted() true

in timer isInterrupted() true

rruptedException: sleep interrupted

at p(Native Method)

at ()

由于在sleep时执行了interrupt()所以会抛出异常。

如果把if(isInterrupted()==true)改成if(rrupted()==true)那么打印的结果就不同了,如下:

...

1582 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

in thread after interrupted() false

还可能是这样:

...

1771 in thread isInterrupted() false

exec interrupt

in timer isInterrupted() true

1772 in thread isInterrupted() false

in thread after interrupted() false

也可能是这样:

...

476 in thread isInterrupted() false

in thread after interrupted() false

in timer isInterrupted() false

虽然执行了interrupt(),但是由于rrupted()可以清除中断,所以并不会抛出异常。

前面我们针对线程是否耗时,给出了停止线程的方法,其实还有一个办法那就是使用()来强行终止线程,不过这个由于该方法不安全已经废弃掉了,因为他有下面两个缺陷:

1. 立即抛出ThreadDeath异常,在线程的run()方法内,任何一点都有可能抛出ThreadDeath异常,包括在catch或finally语句中。

2. 释放该线程所持有的所有的锁。

Tags:JAVA 线程 停止