java中如何停止一個正在運行的線程
如何停止一個正在運行的線程
1)設(shè)置標(biāo)志位
如果線程的run方法中執(zhí)行的是一個重復(fù)執(zhí)行的循環(huán),可以提供一個標(biāo)記來控制循環(huán)是否繼續(xù)
代碼示例:
public class FunDemo02 {
/**
* 練習(xí)2:設(shè)計一個線程:運行10秒后被終止(掌握線程的終止方法)
* @param args
*/
public static void main(String[] args) throws Exception{
MyRunable02 runnable = new MyRunable02();
new Thread(runnable).start();
Thread.sleep(10000); // 主線程休眠10秒鐘
runnable.flag = false;
System.out.println("main、 end ...");
}
}
class MyRunable02 implements Runnable{
boolean flag = true;
@Override
public void run() {
while(flag){
try {
Thread.sleep(1000);
System.out.println(new Date());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 執(zhí)行完成");
}
}2)利用中斷標(biāo)志位
在線程中有個中斷的標(biāo)志位,默認(rèn)是false,當(dāng)我們顯示的調(diào)用 interrupted方法或者isInterrupted方法是會修改標(biāo)志位為true。
我們可以利用此來中斷運行的線程。
代碼示例:
package com.bobo.fundemo;
public class FunDemo07 extends Thread{
public static void main(String[] args) throws InterruptedException {
Thread t1 = new FunDemo07();
t1.start();
Thread.sleep(3000);
t1.interrupt(); // 中斷線程 將中斷標(biāo)志由false修改為了true
// t1.stop(); // 直接就把線程給kill掉了
System.out.println("main .... ");
}
@Override
public void run() {
System.out.println(this.getName() + " start...");
int i = 0 ;
// Thread.interrupted() 如果沒有被中斷 那么是false 如果顯示的執(zhí)行了interrupt 方法就會修改為 true
while(!Thread.interrupted()){
System.out.println(this.getName() + " " + i);
i++;
}
System.out.println(this.getName()+ " end .... ");
}
}3)利用InterruptedException異常
如果線程因為執(zhí)行join(),sleep()或者wait()而進(jìn)入阻塞狀態(tài),此時要想停止它,可以讓他調(diào)用interrupt(),程序會拋出InterruptedException異常。
我們利用這個異常可以來終止線程。
package com.bobo;
public class FunDemo08 extends Thread{
public static void main(String[] args) throws InterruptedException {
Thread t1 = new FunDemo08();
t1.start();
Thread.sleep(3000);
t1.interrupt(); // 中斷線程 將中斷標(biāo)志由false修改為了true
// t1.stop(); // 直接就把線程給kill掉了
System.out.println("main .... ");
}
@Override
public void run() {
System.out.println(this.getName() + " start...");
int i = 0 ;
// Thread.interrupted() 如果沒有被中斷 那么是false 如果顯示的執(zhí)行了interrupt 方法就會修改為 true
while(!Thread.interrupted()){
//while(!Thread.currentThread().isInterrupted()){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
System.out.println(this.getName() + " " + i);
i++;
}
System.out.println(this.getName()+ " end .... ");
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 動態(tài)模擬操作系統(tǒng)進(jìn)程調(diào)度算法
這篇文章主要介紹了采用java語言編程模擬N個進(jìn)程采用動態(tài)高優(yōu)先權(quán)優(yōu)先進(jìn)程調(diào)度算法。文中代碼具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2021-12-12
Java中的HashSet、LinkedHashSet集合解析
這篇文章主要介紹了Java中的HashSet、LinkedHashSet集合解析,與HashSet不同的是,LinkedHashSet在內(nèi)部使用了一個雙向鏈表來維護(hù)元素的順序,因此它可以保持元素的插入順序,這使得LinkedHashSet在需要保持元素順序的場景下非常有用,需要的朋友可以參考下2023-11-11
SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例
本篇文章主要介紹了SSM使用mybatis分頁插件pagehepler實現(xiàn)分頁示例,使用分頁插件的原因,簡化了sql代碼的寫法,實現(xiàn)較好的物理分頁,非常具有實用價值,需要的朋友可以參考下2018-03-03
JavaIO?BufferedReader和BufferedWriter使用及說明
這篇文章主要介紹了JavaIO?BufferedReader和BufferedWriter使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
詳解最簡單易懂的Spring Security 身份認(rèn)證流程講解
這篇文章主要介紹了詳解最簡單易懂的Spring Security 身份認(rèn)證流程講解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
基于Java實現(xiàn)簡易的局域網(wǎng)對話系統(tǒng)
這篇文章主要介紹了基于Java實現(xiàn)的簡易的局域網(wǎng)對話系統(tǒng),文中的示例代碼對我們學(xué)習(xí)Java有一定的幫助,感興趣的小伙伴快來跟隨小編一起學(xué)習(xí)一下吧2021-12-12

