java语言 百分网手机站

java中如何停止线程

时间:2020-09-17 14:22:33 java语言 我要投稿

java中如何停止线程

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

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

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

  class MyThread extends Thread {

  private volatile boolean isStop = false;

  public void run() {

  while (!isStop) {

  System.out.println("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 {

  System.out.println("do something");

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

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

  }

  }

  }

  public void setStop() {

  isStop = true;

  }

  }

  public class Tes1 {

  public static void main(String[] args) {

  MyThread t1 = new MyThread();

  t1.start();

  try {

  Thread.sleep(10);

  t1.setStop();

  t1.interrupt();

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

  这里既然说到了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{

  Thread.sleep(10);

  }catch(InterruptedException ie){

  ie.printStackTrace();

  return;

  }

  while (counter < 2000 &&!done) {

  System.out.println(counter++ + " in thread isInterrupted() "+isInterrupted());

  if(isInterrupted()==true){

  try{

  System.out.println("in thread after interrupted() "+isInterrupted());

  sleep(100);

  break;

  }catch(InterruptedException ie){

  ie.printStackTrace();

  break;

  }

  }

  }

  }

  }

  public class Tes1 {

  public static void main(String[] args) {

  final MyThread t1 = new MyThread();

  t1.start();

  new Timer(true).schedule(new TimerTask() {

  public void run() {

  System.out.println("exec interrupt");

  t1.interrupt();

  System.out.println("in timer isInterrupted() "+t1.isInterrupted());

  }

  }, 20);

  }

  }

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

  ...

  1300 in thread isInterrupted() false

  exec interrupt

  in timer isInterrupted() true

  1301 in thread isInterrupted() true

  in thread after interrupted() true

  java.lang.InterruptedException: sleep interrupted

  at java.lang.Thread.sleep(Native Method)

  at com.example.wd.MyThread.run(Tes1.java:21)

  还可能是这样:

  ...

  1568 in thread isInterrupted() false

  exec interrupt

  in thread after interrupted() true

  in timer isInterrupted() true

  java.lang.InterruptedException: sleep interrupted

  at java.lang.Thread.sleep(Native Method)

  at com.example.wd.MyThread.run(Tes1.java:21)

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

  如果把if(isInterrupted()==true)改成if(Thread.interrupted()==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(),但是由于Thread.interrupted()可以清除中断,所以并不会抛出异常。

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

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

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


【java中如何停止线程】相关文章:

Java线程编程中的主线程详细介绍11-25

浅谈如何使用java多线程12-06

java Runnable接口如何创建线程10-03

java多线程介绍12-07

java多线程教程11-21

java中通用的线程池实例代码11-29

举例讲解Java中的多线程范文欣赏11-25

什么是java主线程12-07

关于Java多线程介绍11-22

Java线程面试题10-05