欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中終止線程的方法詳解

 更新時(shí)間:2017年05月11日 08:55:06   作者:Smile_Miracle  
這篇文章主要介紹了Java中終止線程的方法詳解的相關(guān)資料,需要的朋友可以參考下

Java中終止線程的方式主要有三種:

1、使用stop()方法,已被棄用。原因是:stop()是立即終止,會(huì)導(dǎo)致一些數(shù)據(jù)被到處理一部分就會(huì)被終止,而用戶并不知道哪些數(shù)據(jù)被處理,哪些沒(méi)有被處理,產(chǎn)生了不完整的“殘疾”數(shù)據(jù),不符合完整性,所以被廢棄。So, forget it!

2、使用volatile標(biāo)志位

看一個(gè)簡(jiǎn)單的例子:

首先,實(shí)現(xiàn)一個(gè)Runnable接口,在其中定義volatile標(biāo)志位,在run()方法中使用標(biāo)志位控制程序運(yùn)行

public class MyRunnable implements Runnable { 

 //定義退出標(biāo)志,true會(huì)一直執(zhí)行,false會(huì)退出循環(huán) 
 //使用volatile目的是保證可見(jiàn)性,一處修改了標(biāo)志,處處都要去主存讀取新的值,而不是使用緩存 
 public volatile boolean flag = true; 

 public void run() { 
  System.out.println("第" + Thread.currentThread().getName() + "個(gè)線程創(chuàng)建"); 

  try { 
   Thread.sleep(1000L); 
  } catch (InterruptedException e) { 
   e.printStackTrace(); 
  } 

  //退出標(biāo)志生效位置 
  while (flag) { 
  } 
  System.out.println("第" + Thread.currentThread().getName() + "個(gè)線程終止"); 
 } 
}

然后,在main()方法中創(chuàng)建線程,在合適的時(shí)候,修改標(biāo)志位,終止運(yùn)行中的線程。

public class TreadTest { 
 public static void main(String[] arg) throws InterruptedException { 
  MyRunnable runnable = new MyRunnable(); 

  //創(chuàng)建3個(gè)線程 
  for (int i = 1; i <= 3; i++) { 
   Thread thread = new Thread(runnable, i + ""); 
   thread.start(); 
  } 
  //線程休眠 
  Thread.sleep(2000L); 
  System.out.println("——————————————————————————"); 
  //修改退出標(biāo)志,使線程終止 
  runnable.flag = false; 
 } 
}

最后,運(yùn)行結(jié)果,如下:

第1個(gè)線程創(chuàng)建
第2個(gè)線程創(chuàng)建
第3個(gè)線程創(chuàng)建
--------------------------
第3個(gè)線程終止
第1個(gè)線程終止
第2個(gè)線程終止

3、使用interrupt()中斷的方式,注意使用interrupt()方法中斷正在運(yùn)行中的線程只會(huì)修改中斷狀態(tài)位,可以通過(guò)isInterrupted()判斷。如果使用interrupt()方法中斷阻塞中的線程,那么就會(huì)拋出InterruptedException異常,可以通過(guò)catch捕獲異常,然后進(jìn)行處理后終止線程。有些情況,我們不能判斷線程的狀態(tài),所以使用interrupt()方法時(shí)一定要慎重考慮。

第一種:正在運(yùn)行中終止

public class MyThread extends Thread {
 public void run(){
  super.run();
  try {
   for(int i=0; i<500000; i++){
    if(this.interrupted()) {
     System.out.println("線程已經(jīng)終止, for循環(huán)不再執(zhí)行");
      throw new InterruptedException();
    }
    System.out.println("i="+(i+1));
   }

   System.out.println("這是for循環(huán)外面的語(yǔ)句,也會(huì)被執(zhí)行");
  } catch (InterruptedException e) {
   System.out.println("進(jìn)入MyThread.java類中的catch了。。。");
   e.printStackTrace();
  }
 }
}

public class Run {
 public static void main(String args[]){
  Thread thread = new MyThread();
  thread.start();
  try {
   Thread.sleep(2000);
   thread.interrupt();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }
}

運(yùn)行結(jié)果如下:

...
i=203798
i=203799
i=203800
線程已經(jīng)終止, for循環(huán)不再執(zhí)行
進(jìn)入MyThread.java類中的catch了。。。
java.lang.InterruptedException
 at thread.MyThread.run(MyThread.java:13)

第二種:阻塞狀態(tài)(sleep,wait等)終止

public class MyThread extends Thread {
 public void run(){
  super.run();

  try {
   System.out.println("線程開(kāi)始。。。");
   Thread.sleep(200000);
   System.out.println("線程結(jié)束。");
  } catch (InterruptedException e) {
   System.out.println("在沉睡中被停止, 進(jìn)入catch, 調(diào)用isInterrupted()方法的結(jié)果是:" + this.isInterrupted());
   e.printStackTrace();
  }

 }
}

線程開(kāi)始。。。
在沉睡中被停止, 進(jìn)入catch, 調(diào)用isInterrupted()方法的結(jié)果是:false
java.lang.InterruptedException: sleep interrupted
 at java.lang.Thread.sleep(Native Method)
 at thread.MyThread.run(MyThread.java:12)

從打印的結(jié)果來(lái)看, 如果在sleep狀態(tài)下停止某一線程,會(huì)進(jìn)入catch語(yǔ)句,并且清除停止?fàn)顟B(tài)值,使之變?yōu)閒alse。

前一個(gè)實(shí)驗(yàn)是先sleep然后再用interrupt()停止,與之相反的操作在學(xué)習(xí)過(guò)程中也要注意:

public class MyThread extends Thread {
 public void run(){
  super.run();
  try {
   System.out.println("線程開(kāi)始。。。");
   for(int i=0; i<10000; i++){
    System.out.println("i=" + i);
   }
   Thread.sleep(200000);
   System.out.println("線程結(jié)束。");
  } catch (InterruptedException e) {
    System.out.println("先停止,再遇到sleep,進(jìn)入catch異常");
   e.printStackTrace();
  }

 }
}

public class Run {
 public static void main(String args[]){
  Thread thread = new MyThread();
  thread.start();
  thread.interrupt();
 }
}

運(yùn)行結(jié)果:

i=9998
i=9999
先停止,再遇到sleep,進(jìn)入catch異常
java.lang.InterruptedException: sleep interrupted
 at java.lang.Thread.sleep(Native Method)
 at thread.MyThread.run(MyThread.java:15)

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    Chrome 51 開(kāi)始,瀏覽器的 Cookie 新增加了一個(gè)SameSite屬性,用來(lái)防止 CSRF 攻擊和用戶追蹤。今天通過(guò)本文給大家介紹Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性,感興趣的朋友一起看看吧
    2022-01-01
  • Java集合繼承體系詳解

    Java集合繼承體系詳解

    這篇文章主要為大家詳細(xì)介紹了Java集合繼承體系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java 14 發(fā)布了,你還會(huì)使用Lombok?

    Java 14 發(fā)布了,你還會(huì)使用Lombok?

    2020年3月17日發(fā)布,Java正式發(fā)布了JDK 14 ,目前已經(jīng)可以開(kāi)放下載。在JDK 14中,共有16個(gè)新特性,本文主要來(lái)介紹其中的一個(gè)特性:JEP 359: Records,需要的朋友可以參考下
    2020-04-04
  • 使用Feign擴(kuò)展包實(shí)現(xiàn)微服務(wù)間文件上傳

    使用Feign擴(kuò)展包實(shí)現(xiàn)微服務(wù)間文件上傳

    這篇文章主要為大家詳細(xì)介紹了使用Feign擴(kuò)展包實(shí)現(xiàn)微服務(wù)間文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Java創(chuàng)建和啟動(dòng)線程的兩種方式實(shí)例分析

    Java創(chuàng)建和啟動(dòng)線程的兩種方式實(shí)例分析

    這篇文章主要介紹了Java創(chuàng)建和啟動(dòng)線程的兩種方式,結(jié)合實(shí)例形式分析了java多線程創(chuàng)建、使用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • Java錯(cuò)誤問(wèn)題:找不到或無(wú)法加載主類的解決

    Java錯(cuò)誤問(wèn)題:找不到或無(wú)法加載主類的解決

    這篇文章主要介紹了Java錯(cuò)誤問(wèn)題:找不到或無(wú)法加載主類的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • springboot+feign+Hystrix整合(親測(cè)有效)

    springboot+feign+Hystrix整合(親測(cè)有效)

    本文主要介紹了springboot+feign+Hystrix整合,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • SpringMVC多線程下無(wú)法獲取請(qǐng)求的原因和解決方法

    SpringMVC多線程下無(wú)法獲取請(qǐng)求的原因和解決方法

    從問(wèn)題出發(fā),逐步講透在SpringMVC中使用RequestContextHolder對(duì)象在多線程情況下無(wú)法獲取請(qǐng)求的真實(shí)原因,本文將和大家一起深入剖析SpringMVC多線程下無(wú)法獲取請(qǐng)求的原因和解決,需要的朋友可以參考下
    2024-04-04
  • Java實(shí)現(xiàn)XML格式與JSON格式互相轉(zhuǎn)換的方法

    Java實(shí)現(xiàn)XML格式與JSON格式互相轉(zhuǎn)換的方法

    這篇文章主要介紹了Java實(shí)現(xiàn)XML格式與JSON格式互相轉(zhuǎn)換的方法,方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),選擇使用哪種格式通常取決于項(xiàng)目的需求和上下文,所以格式轉(zhuǎn)換就成了我們必備的技能,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2023-10-10
  • 一次由Lombok的@AllArgsConstructor注解引發(fā)的錯(cuò)誤及解決

    一次由Lombok的@AllArgsConstructor注解引發(fā)的錯(cuò)誤及解決

    這篇文章主要介紹了一次由Lombok的@AllArgsConstructor注解引發(fā)的錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論