日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

用interrupt()中断Java线程

發(fā)布時間:2024/4/17 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用interrupt()中断Java线程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

http://hapinwater.iteye.com/blog/310558

最近在學習Java線程相關(guān)的東西,和大家分享一下,有錯誤之處歡迎大家指正.

假如我們有一個任務(wù)如下,交給一個Java線程來執(zhí)行,如何才能保證調(diào)用interrupt()來中斷它呢?

Java代碼
  • class ATask implements Runnable{?
  • ?
  • ??? private double d = 0.0;?
  • ?????
  • ??? public void run() {?
  • ??????? //死循環(huán)執(zhí)行打印"I am running!" 和做消耗時間的浮點計算?
  • ??????? while (true) {?
  • ??????????? System.out.println("I am running!");?
  • ?????????????
  • ??????????? for (int i = 0; i < 900000; i++) {?
  • ??????????????? d =? d + (Math.PI + Math.E) / d;?
  • ??????????? }?
  • ??????????? //給線程調(diào)度器可以切換到其它進程的信號?
  • ??????????? Thread.yield();?
  • ??????? }?
  • ??? }?
  • }?
  • ?
  • public class InterruptTaskTest {?
  • ?????
  • ??? public static void main(String[] args) throws Exception{?
  • ??????? //將任務(wù)交給一個線程執(zhí)行?
  • ??????? Thread t = new Thread(new ATask());?
  • ??????? t.start();?
  • ?????????
  • ??????? //運行一斷時間中斷線程?
  • ??????? Thread.sleep(100);?
  • ??????? System.out.println("****************************");?
  • ??????? System.out.println("Interrupted Thread!");?
  • ??????? System.out.println("****************************");?
  • ??????? t.interrupt();?
  • ??? }?
  • }??
  • class ATask implements Runnable{private double d = 0.0;public void run() {//死循環(huán)執(zhí)行打印"I am running!" 和做消耗時間的浮點計算while (true) {System.out.println("I am running!");for (int i = 0; i < 900000; i++) {d = d + (Math.PI + Math.E) / d;}//給線程調(diào)度器可以切換到其它進程的信號Thread.yield();}} }public class InterruptTaskTest {public static void main(String[] args) throws Exception{//將任務(wù)交給一個線程執(zhí)行Thread t = new Thread(new ATask());t.start();//運行一斷時間中斷線程Thread.sleep(100);System.out.println("****************************");System.out.println("Interrupted Thread!");System.out.println("****************************");t.interrupt();} }



    運行這個程序,我們發(fā)現(xiàn)調(diào)用interrupt()后,程序仍在運行,如果不強制結(jié)束,程序?qū)⒁恢边\行下去,如下所示:

    Java代碼
  • ......?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • ****************************?
  • Interrupted Thread!?
  • ****************************?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • ....?
  • ...... I am running! I am running! I am running! I am running! **************************** Interrupted Thread! **************************** I am running! I am running! I am running! I am running! I am running! ....


    雖然中斷發(fā)生了,但線程仍然在進行,離開線程有兩種常用的方法:
    拋出InterruptedException和用Thread.interrupted()檢查是否發(fā)生中斷,下面分別看一下這兩種方法:
    1.在阻塞操作時如Thread.sleep()時被中斷會拋出InterruptedException(注意,進行不能中斷的IO操作而阻塞和要獲得對象的鎖調(diào)用對象的synchronized方法而阻塞時不會拋出InterruptedException)

    Java代碼
  • class ATask implements Runnable{?
  • ?
  • ??? private double d = 0.0;?
  • ?????
  • ??? public void run() {?
  • ??????? //死循環(huán)執(zhí)行打印"I am running!" 和做消耗時間的浮點計算?
  • ??????? try {?
  • ??????????? while (true) {?
  • ??????????????? System.out.println("I am running!");?
  • ?????????????????
  • ??????????????? for (int i = 0; i < 900000; i++) {?
  • ??????????????????? d =? d + (Math.PI + Math.E) / d;?
  • ??????????????? }?
  • ??????????????? //休眠一斷時間,中斷時會拋出InterruptedException?
  • ??????????????? Thread.sleep(50);?
  • ??????????? }?
  • ??????? } catch (InterruptedException e) {?
  • ??????????? System.out.println("ATask.run() interrupted!");?
  • ??????? }?
  • ??? }?
  • }?
  • class ATask implements Runnable{private double d = 0.0;public void run() {//死循環(huán)執(zhí)行打印"I am running!" 和做消耗時間的浮點計算try {while (true) {System.out.println("I am running!");for (int i = 0; i < 900000; i++) {d = d + (Math.PI + Math.E) / d;}//休眠一斷時間,中斷時會拋出InterruptedExceptionThread.sleep(50);}} catch (InterruptedException e) {System.out.println("ATask.run() interrupted!");}} }


    程序運行結(jié)果如下:

    Java代碼
  • I am running!?
  • I am running!?
  • ****************************?
  • Interrupted Thread!?
  • ****************************?
  • ATask.run() interrupted!?
  • I am running! I am running! **************************** Interrupted Thread! **************************** ATask.run() interrupted!


    可以看到中斷任務(wù)時讓任務(wù)拋出InterruptedException來離開任務(wù).

    2.Thread.interrupted()檢查是否發(fā)生中斷.Thread.interrupted()能告訴你線程是否發(fā)生中斷,并將清除中斷狀態(tài)標記,所以程序不會兩次通知你線程發(fā)生了中斷.

    Java代碼
  • class ATask implements Runnable{?
  • ?
  • ??? private double d = 0.0;?
  • ?????
  • ??? public void run() {?
  • ?????????
  • ??????? //檢查程序是否發(fā)生中斷?
  • ??????? while (!Thread.interrupted()) {?
  • ??????????? System.out.println("I am running!");?
  • ?
  • ??????????? for (int i = 0; i < 900000; i++) {?
  • ??????????????? d = d + (Math.PI + Math.E) / d;?
  • ??????????? }?
  • ??????? }?
  • ?
  • ??????? System.out.println("ATask.run() interrupted!");?
  • ??? }?
  • }?
  • class ATask implements Runnable{private double d = 0.0;public void run() {//檢查程序是否發(fā)生中斷while (!Thread.interrupted()) {System.out.println("I am running!");for (int i = 0; i < 900000; i++) {d = d + (Math.PI + Math.E) / d;}}System.out.println("ATask.run() interrupted!");} }


    程序運行結(jié)果如下:

    Java代碼
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • I am running!?
  • ****************************?
  • Interrupted Thread!?
  • ****************************?
  • ATask.run() interrupted!?
  • I am running! I am running! I am running! I am running! I am running! I am running! I am running! **************************** Interrupted Thread! **************************** ATask.run() interrupted!



    我們可結(jié)合使用兩種方法來達到可以通過interrupt()中斷線程.請看下面例子:

    Java代碼
  • class ATask implements Runnable{?
  • ?
  • ??? private double d = 0.0;?
  • ?????
  • ??? public void run() {?
  • ?????????
  • ??????? try {?
  • ??????? //檢查程序是否發(fā)生中斷?
  • ??????? while (!Thread.interrupted()) {?
  • ??????????? System.out.println("I am running!");?
  • ??????????? //point1 before sleep?
  • ??????????? Thread.sleep(20);?
  • ??????????? //point2 after sleep?
  • ??????????? System.out.println("Calculating");?
  • ??????????? for (int i = 0; i < 900000; i++) {?
  • ??????????????? d = d + (Math.PI + Math.E) / d;?
  • ??????????? }?
  • ??????? }?
  • ?????????
  • ??????? } catch (InterruptedException e) {?
  • ??????????? System.out.println("Exiting by Exception");?
  • ??????? }?
  • ?????????
  • ??????? System.out.println("ATask.run() interrupted!");?
  • ??? }?
  • }?
  • class ATask implements Runnable{private double d = 0.0;public void run() {try {//檢查程序是否發(fā)生中斷while (!Thread.interrupted()) {System.out.println("I am running!");//point1 before sleepThread.sleep(20);//point2 after sleepSystem.out.println("Calculating");for (int i = 0; i < 900000; i++) {d = d + (Math.PI + Math.E) / d;}}} catch (InterruptedException e) {System.out.println("Exiting by Exception");}System.out.println("ATask.run() interrupted!");} }


    在point1之前處point2之后發(fā)生中斷會產(chǎn)生兩種不同的結(jié)果,可以通過修改InterruptTaskTest main()里的Thread.sleep()的時間來達到在point1之前產(chǎn)生中斷或在point2之后產(chǎn)生中斷.
    如果在point1之前發(fā)生中斷,程序會在調(diào)用Thread.sleep()時拋出InterruptedException從而結(jié)束線程.這和在Thread.sleep()時被中斷是一樣的效果.程序運行結(jié)果可能如下:

    Java代碼
  • I am running!?
  • Calculating?
  • I am running!?
  • Calculating?
  • I am running!?
  • Calculating?
  • I am running!?
  • ****************************?
  • Interrupted Thread!?
  • ****************************?
  • Exiting by Exception?
  • ATask.run() interrupted!?
  • I am running! Calculating I am running! Calculating I am running! Calculating I am running! **************************** Interrupted Thread! **************************** Exiting by Exception ATask.run() interrupted!


    如果在point2之后發(fā)生中斷,線程會繼續(xù)執(zhí)行到下一次while判斷中斷狀態(tài)時.程序運行結(jié)果可能如下:

    Java代碼
  • I am running!?
  • Calculating?
  • I am running!?
  • Calculating?
  • I am running!?
  • Calculating?
  • ****************************?
  • Interrupted Thread!?
  • ****************************?
  • ATask.run() interrupted!?
  • ?

    總結(jié)

    以上是生活随笔為你收集整理的用interrupt()中断Java线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。