av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語(yǔ)言

java中如何停止線程

時(shí)間:2025-03-03 22:16:26 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

java中如何停止線程

  Java是一門(mén)面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念。想知道java中如何停止線程?下面就一起來(lái)了解看看吧!

  一般來(lái)說(shuō)線程執(zhí)行完run()之后就自動(dòng)結(jié)束了,不過(guò)有些時(shí)候我們需要線程不停的做一些事情,也就是使用while循環(huán),那么這時(shí)候該如何停止線程呢?

  這個(gè)問(wèn)題需要分情況來(lái)討論,如果線程做的事情不是耗時(shí)的,那么只需要使用一個(gè)標(biāo)志即可,具體的代碼如下:

  class MyThread extends Thread {

  private volatile boolean isStop = false;

  public void run() {

  while (!isStop) {

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

  }

  }

  public void setStop() {

  isStop = true;

  }

  }

  如果需要退出時(shí),調(diào)用setStop()即可。這里使用了一個(gè)Java關(guān)鍵字volatile,這個(gè)關(guān)鍵字的目的是使isStop同步,也就是說(shuō)在同一時(shí)刻只能由一個(gè)線程來(lái)修改isStop的值。

  如果線程做的事情是耗時(shí)或者說(shuō)阻塞的(如調(diào)用了sleep,同步鎖的wait,socket的receiver,accept等方法),那么就需要用到interrupt()了,調(diào)用該函數(shù)時(shí)會(huì)拋出InterruptedException異常,代碼中通過(guò)捕獲該異常,然后break出循環(huán),就可以了。代碼如下:

  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;//如果沒(méi)有這一句,而且不調(diào)用setStop(),線程并不會(huì)結(jié)束,需要特別注意一下

  }

  }

  }

  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();

  }

  }

  }

  這里既然說(shuō)到了interrupt(),那么就不得不提另外兩個(gè)函數(shù):interrupted()和isInterrupted?聪逻@兩個(gè)函數(shù)的作用:

  public static boolean interrupted()

  測(cè)試當(dāng)前線程是否已經(jīng)中斷,并且清除線程的中斷狀態(tài)。

  public boolean isInterrupted()

  測(cè)試線程是否已經(jīng)中斷,線程的中斷狀態(tài)不受該方法的影響。

  這兩個(gè)函數(shù)的返回值含義:

  如果當(dāng)前線程已經(jīng)中斷,則返回 true;否則返回 false。

  其實(shí)當(dāng)線程阻塞并且調(diào)用了interrupt()時(shí),不止是拋出InterruptedException異常還會(huì)調(diào)用interrupted()來(lái)清除線程的中斷狀態(tài),所以在catch里面調(diào)用isInterrupted()會(huì)返回false。

  為了更好的理解這兩個(gè)函數(shù),我們看個(gè)例子:

  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);

  }

  }

  執(zhí)行效果如下(每一次執(zhí)行打印的都不一樣,因?yàn)闆](méi)有做線程同步):

  ...

  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時(shí)執(zhí)行了interrupt()所以會(huì)拋出異常。

  如果把if(isInterrupted()==true)改成if(Thread.interrupted()==true)那么打印的結(jié)果就不同了,如下:

  ...

  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

  雖然執(zhí)行了interrupt(),但是由于Thread.interrupted()可以清除中斷,所以并不會(huì)拋出異常。

  前面我們針對(duì)線程是否耗時(shí),給出了停止線程的方法,其實(shí)還有一個(gè)辦法那就是使用thread.stop()來(lái)強(qiáng)行終止線程,不過(guò)這個(gè)由于該方法不安全已經(jīng)廢棄掉了,因?yàn)樗邢旅鎯蓚(gè)缺陷:

  1. 立即拋出ThreadDeath異常,在線程的run()方法內(nèi),任何一點(diǎn)都有可能拋出ThreadDeath異常,包括在catch或finally語(yǔ)句中。

  2. 釋放該線程所持有的所有的鎖。


【java中如何停止線程】相關(guān)文章:

java如何正確停止一個(gè)線程05-25

如何使用java多線程05-04

如何創(chuàng)建并運(yùn)行Java線程01-11

JAVA中終止線程的方法08-12

Java線程編程中的主線程詳細(xì)介紹01-31

Java 如何進(jìn)行線程同步05-19

java Runnable接口如何創(chuàng)建線程05-18

淺談如何使用java多線程05-07

java的多線程04-09