日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 线程停止在那个为止_java停止线程

發(fā)布時(shí)間:2025/3/12 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 线程停止在那个为止_java停止线程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文將介紹jdk提供的api中停止線程的用法。

停止一個(gè)線程意味著在一個(gè)線程執(zhí)行完任務(wù)之前放棄當(dāng)前的操作,停止一個(gè)線程可以使用Thread.stop()方法,但是做好不要使用它,它是后繼jdk版本中廢棄的或者將不能使用的方法,大多數(shù)停止一個(gè)線程的操作使用Thread.interrupt()方法。

1.本實(shí)例將調(diào)用interrupt()方法來停止線程,創(chuàng)建MyThread.java,代碼如下:

package com.lit.thread006;

public class MyThread extends Thread{

@Override

public void run() {

for(int i = 0 ; i < 500000; i++){

System.out.println("i= "+(i));

}

}

}

再創(chuàng)建Run1.java,代碼如下:

package com.lit.thread006;

public class Run1 {

public static void main(String[] args) {

try {

MyThread thread = new MyThread() ;

thread.start();

Thread.sleep(1000);

thread.interrupt();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

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

i= 499992

i= 499993

i= 499994

i= 499995

i= 499996

i= 499997

i= 499998

i= 499999

程序在執(zhí)行thread.interrupt()之后,線程并未停下來,而是繼續(xù)執(zhí)行直到打印了50萬行記錄才結(jié)束方法。顯然,interrupt并沒有停止線程。

Thread.java類提供了判斷線程狀態(tài)的方法:

(1)this.interrupted(): 測(cè)試當(dāng)前的線程是否已經(jīng)中斷;

(2)this.isInterrupted():測(cè)試線程是否已經(jīng)中斷;

兩個(gè)方法的區(qū)別是:第一個(gè)方法判斷的是當(dāng)前的線程,是一個(gè)靜態(tài)的方法,第二個(gè)方式是判斷調(diào)用的線程:

2.創(chuàng)建Run2.java,代碼如下:

package com.lit.thread006;

public class Run2 {

public static void main(String[] args) {

try {

MyThread thread = new MyThread() ;

thread.start();

Thread.sleep(1000);

thread.interrupt();

System.out.println("1測(cè)試停止?fàn)顟B(tài)?"+thread.interrupted());

System.out.println("2測(cè)試停止?fàn)顟B(tài)?"+thread.interrupted());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

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

i= 195000

i= 195001

i= 195002

1測(cè)試停止?fàn)顟B(tài)?false

i= 195003

i= 195004

i= 195116

i= 195117

2測(cè)試停止?fàn)顟B(tài)?false

i= 195118

i= 195119

i= 195120

輸出結(jié)果是兩個(gè)false,因?yàn)楫?dāng)前線程是main,它從未停止。

3.創(chuàng)建Run3.java,代碼如下:

package com.lit.thread006;

public class Run3 {

public static void main(String[] args) {

Thread.currentThread().interrupt();

System.out.println("1是否停止?"+Thread.interrupted());

System.out.println("2是否停止?"+Thread.interrupted());

}

}

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

1是否停止?true

2是否停止?false

可以看到第二個(gè)值是false,那是因?yàn)樵僬{(diào)用了interrupted()方法之后,返回線程的中斷狀態(tài),并且清除該狀態(tài)。

4.繼續(xù)看一下isInterrupted()方法,創(chuàng)建Run4.java,代碼如下:

package com.lit.thread006;

public class Run4 {

public static void main(String[] args) {

try {

MyThread thread = new MyThread();

thread.start();

Thread.sleep(2000);

thread.interrupt();

System.out.println("是否停止1?"+thread.isInterrupted());

System.out.println("是否停止2?"+thread.isInterrupted());

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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

i= 405655

i= 405656

是否停止1?true

是否停止2?true

i= 405657

i= 405658

可以看到,isInterrupted()并未清除中斷狀態(tài)。

5.線程執(zhí)行interrupt()之后停不下來只是做了一個(gè)停止標(biāo)記,并不是真正的停止線程,那么如何停止一個(gè)線程呢?

創(chuàng)建StopThread.java類,代碼如下:

package com.lit.thread006;

public class StopThread extends Thread{

@Override

public void run() {

try {

for(int i = 0 ; i < 500000 ; i++){

if(this.interrupted()){

System.out.println("線程已經(jīng)是中斷狀態(tài),線程退出!");

throw new InterruptedException();

}

System.out.println("i = "+(i+1));

}

} catch (InterruptedException e) {

System.out.println("進(jìn)入了StopThread的catch代碼塊");

e.printStackTrace();

}

}

public static void main(String[] args) {

//測(cè)試

try {

StopThread thread = new StopThread() ;

thread.start();

Thread.sleep(1000);

thread.interrupt();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("method end");

}

}

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

i = 205738

i = 205739

i = 205740

i = 205741

method end

線程已經(jīng)是中斷狀態(tài),線程退出!

進(jìn)入了StopThread的catch代碼塊

java.lang.InterruptedException

at com.lit.thread006.StopThread.run(StopThread.java:10)

通過拋出異常讓線程停止。

此外還可以通過return關(guān)鍵字停止異常,也可以使用stop()方法停止異常,但要注意的是stop()方法并不推薦使用,因?yàn)閟top()方法釋放鎖將會(huì)給數(shù)據(jù)造成不一致的結(jié)果,停止方式太暴力。

總結(jié)

以上是生活随笔為你收集整理的java 线程停止在那个为止_java停止线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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