面試題:Java中如何停止線程的方法
如何停止線程是Java并發(fā)面試中的常見(jiàn)問(wèn)題,本篇文章將從答題思路到答題細(xì)節(jié)給出一些參考。
答題思路:
- 停止線程的正確方式是使用中斷
- 想停止線程需要停止方,被停止方,被停止方的子方法相互配合
- 擴(kuò)展到常見(jiàn)的錯(cuò)誤停止線程方法:已被廢棄的stop/suspend,無(wú)法喚醒阻塞線程的volatile
1. 正確方式是中斷
其實(shí)從邏輯上也很好理解的,一個(gè)線程正在運(yùn)行,如何讓他停止?
A. 從外部直接調(diào)用該線程的stop方法,直接把線程停下來(lái)。
B. 從外部通過(guò)中斷通知線程停止,然后切換到被停止的線程,該線程執(zhí)行一系列邏輯后自己停止。
很明顯B方法要比A方法好很多,A方法太暴力了,你根本不知道被停止的線程在執(zhí)行什么任務(wù)就直接把他停止了,程序容易出問(wèn)題;而B(niǎo)方法把線程停止交給線程本身去做,被停止的線程可以在自己的代碼中進(jìn)行一些現(xiàn)場(chǎng)保護(hù)或者打印錯(cuò)誤日志等方法再停止,更加合理,程序也更具健壯性。
下面要講的是線程如何能夠響應(yīng)中斷,第一個(gè)方法是通過(guò)循環(huán)不斷判斷自身是否產(chǎn)生了中斷:
public class Demo1 implements Runnable{
@Override
public void run() {
int num = 0;
while(num <= Integer.MAX_VALUE / 2 && !Thread.currentThread().isInterrupted()){
if(num % 10000 == 0){
System.out.println(num);
}
num++;
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo1());
thread.start();
thread.sleep(1000);
thread.interrupt();
}
}
在上面的代碼中,我們?cè)谘h(huán)條件中不斷判斷線程本身是否產(chǎn)生了中斷,如果產(chǎn)生了中斷就不再打印
還有一個(gè)方法是通過(guò)java內(nèi)定的機(jī)制響應(yīng)中斷:當(dāng)線程調(diào)用sleep(),wait()方法后進(jìn)入阻塞后,如果線程在阻塞的過(guò)程中被中斷了,那么線程會(huì)捕獲或拋出一個(gè)中斷異常,我們可以根據(jù)這個(gè)中斷異常去控制線程的停止。具體代碼如下:
public class Demo3 implements Runnable {
@Override
public void run() {
int num = 0;
try {
while(num < Integer.MAX_VALUE / 2){
if(num % 100 == 0){
System.out.println(num);
}
num++;
Thread.sleep(10);
}
} catch (InterruptedException e) {//捕獲中斷異常,在本代碼中,出現(xiàn)中斷異常后將退出循環(huán)
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo3());
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
2. 各方配合才能完美停止
在上面的兩段代碼中已經(jīng)可以看到,想通過(guò)中斷停止線程是個(gè)需要多方配合。上面已經(jīng)演示了中斷方和被中斷方的配合,下面考慮更多的情況:假如要被停止的線程正在執(zhí)行某個(gè)子方法,這個(gè)時(shí)候該如何處理中斷?
有兩個(gè)辦法:第一個(gè)是把中斷傳遞給父方法,第二個(gè)是重新設(shè)置當(dāng)前線程為中斷。
先說(shuō)第一個(gè)例子:在子方法中把中斷異常上拋給父方法,然后在父方法中處理中斷:
public class Demo4 implements Runnable{
@Override
public void run() {
try{//在父方法中捕獲中斷異常
while(true){
System.out.println("go");
throwInterrupt();
}
}catch (InterruptedException e) {
e.printStackTrace();
System.out.println("檢測(cè)到中斷,保存錯(cuò)誤日志");
}
}
private void throwInterrupt() throws InterruptedException {//把中斷上傳給父方法
Thread.sleep(2000);
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo4());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
第二個(gè)例子:在子方法中捕獲中斷異常,但是捕獲以后當(dāng)前線程的中斷控制位將被清除,父方法執(zhí)行時(shí)將無(wú)法感知中斷。所以此時(shí)在子方法中重新設(shè)置中斷,這樣父方法就可以通過(guò)對(duì)中斷控制位的判斷來(lái)處理中斷:
public class Demo5 implements Runnable{
@Override
public void run() {
while(true && !Thread.currentThread().isInterrupted()){//每次循環(huán)判斷中斷控制位
System.out.println("go");
throwInterrupt();
}
System.out.println("檢測(cè)到了中斷,循環(huán)打印退出");
}
private void throwInterrupt(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();//重新設(shè)置中斷
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo5());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
講到這里,正確的停止線程方法已經(jīng)講的差不多了,下面我們看一下常見(jiàn)的錯(cuò)誤停止線程的例子:
3. 常見(jiàn)錯(cuò)誤停止線程例子:
這里介紹兩種常見(jiàn)的錯(cuò)誤,先說(shuō)比較好理解的一種,也就是開(kāi)頭所說(shuō)的,在外部直接把運(yùn)行中的線程停止掉。這種暴力的方法很有可能造成臟數(shù)據(jù)。
看下面的例子:
public class Demo6 implements Runnable{
/**
* 模擬指揮軍隊(duì),以一個(gè)連隊(duì)為單位領(lǐng)取武器,一共有5個(gè)連隊(duì),一個(gè)連隊(duì)10個(gè)人
*/
@Override
public void run() {
for(int i = 0; i < 5; i++){
System.out.println("第" + (i + 1) + "個(gè)連隊(duì)開(kāi)始領(lǐng)取武器");
for(int j = 0; j < 10; j++){
System.out.println("第" + (j + 1) + "個(gè)士兵領(lǐng)取武器");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("第" + (i + 1) + "個(gè)連隊(duì)領(lǐng)取武器完畢");
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo6());
thread.start();
Thread.sleep(2500);
thread.stop();
}
}
在上面的例子中,我們模擬軍隊(duì)發(fā)放武器,規(guī)定一個(gè)連為一個(gè)單位,每個(gè)連有10個(gè)人。當(dāng)我們直接從外部通過(guò)stop方法停止武器發(fā)放后。很有可能某個(gè)連隊(duì)正處于發(fā)放武器的過(guò)程中,導(dǎo)致部分士兵沒(méi)有領(lǐng)到武器。
這就好比在生產(chǎn)環(huán)境中,銀行以10筆轉(zhuǎn)賬為一個(gè)單位進(jìn)行轉(zhuǎn)賬,如果線程在轉(zhuǎn)賬的中途被突然停止,那么很可能會(huì)造成臟數(shù)據(jù)。
另外一個(gè)“常見(jiàn)”錯(cuò)誤可能知名度不是太高,就是:通過(guò)volatile關(guān)鍵字停止線程。具體來(lái)說(shuō)就是通過(guò)volatile關(guān)鍵字定義一個(gè)變量,通過(guò)判斷變量來(lái)停止線程。這個(gè)方法表面上是沒(méi)問(wèn)題的,我們先看這個(gè)表面的例子:
public class Demo7 implements Runnable {
private static volatile boolean canceled = false;
@Override
public void run() {
int num = 0;
while(num <= Integer.MAX_VALUE / 2 && !canceled){
if(num % 100 == 0){
System.out.println(num + "是100的倍數(shù)");
}
num++;
}
System.out.println("退出");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Demo7());
thread.start();
Thread.sleep(1000);
canceled = true;
}
}
但是這個(gè)方法有一個(gè)潛在的大漏洞,就是若線程進(jìn)入了阻塞狀態(tài),我們將不能通過(guò)修改volatile變量來(lái)停止線程,看下面的生產(chǎn)者消費(fèi)者例子:
/**
* 通過(guò)生產(chǎn)者消費(fèi)者模式演示volatile的局限性,volatile不能喚醒已經(jīng)阻塞的線程
* 生產(chǎn)者生產(chǎn)速度很快,消費(fèi)者消費(fèi)速度很慢,通過(guò)阻塞隊(duì)列存儲(chǔ)商品
*/
public class Demo8 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue storage = new ArrayBlockingQueue(10);
Producer producer = new Producer(storage);
Thread producerThread = new Thread(producer);
producerThread.start();
Thread.sleep(1000);//1s足夠讓生產(chǎn)者把阻塞隊(duì)列塞滿
Consumer consumer = new Consumer(storage);
while(consumer.needMoreNums()){
System.out.println(storage.take() + "被消費(fèi)");
Thread.sleep(100);//讓消費(fèi)者消費(fèi)慢一點(diǎn),給生產(chǎn)者生產(chǎn)的時(shí)間
}
System.out.println("消費(fèi)者消費(fèi)完畢");
producer.canceled = true;//讓生產(chǎn)者停止生產(chǎn)(實(shí)際情況是不行的,因?yàn)榇藭r(shí)生產(chǎn)者處于阻塞狀態(tài),volatile不能喚醒阻塞狀態(tài)的線程)
}
}
class Producer implements Runnable{
public volatile boolean canceled = false;
private BlockingQueue storage;
public Producer(BlockingQueue storage) {
this.storage = storage;
}
@Override
public void run() {
int num = 0;
try{
while(num < Integer.MAX_VALUE / 2 && !canceled){
if(num % 100 == 0){
this.storage.put(num);
System.out.println(num + "是100的倍數(shù),已經(jīng)被放入倉(cāng)庫(kù)");
}
num++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("生產(chǎn)者停止生產(chǎn)");
}
}
}
class Consumer{
private BlockingQueue storage;
public Consumer(BlockingQueue storage) {
this.storage = storage;
}
public boolean needMoreNums(){
return Math.random() < 0.95 ? true : false;
}
}
上面的例子運(yùn)行后會(huì)發(fā)現(xiàn)生產(chǎn)線程一直不能停止,因?yàn)樗幱谧枞麪顟B(tài),當(dāng)消費(fèi)者線程退出后,沒(méi)有任何東西能喚醒生產(chǎn)者線程。
這種錯(cuò)誤用中斷就很好解決:
/**
* 通過(guò)生產(chǎn)者消費(fèi)者模式演示volatile的局限性,volatile不能喚醒已經(jīng)阻塞的線程
* 生產(chǎn)者生產(chǎn)速度很快,消費(fèi)者消費(fèi)速度很慢,通過(guò)阻塞隊(duì)列存儲(chǔ)商品
*/
public class Demo8 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue storage = new ArrayBlockingQueue(10);
Producer producer = new Producer(storage);
Thread producerThread = new Thread(producer);
producerThread.start();
Thread.sleep(1000);//1s足夠讓生產(chǎn)者把阻塞隊(duì)列塞滿
Consumer consumer = new Consumer(storage);
while(consumer.needMoreNums()){
System.out.println(storage.take() + "被消費(fèi)");
Thread.sleep(100);//讓消費(fèi)者消費(fèi)慢一點(diǎn),給生產(chǎn)者生產(chǎn)的時(shí)間
}
System.out.println("消費(fèi)者消費(fèi)完畢");
producerThread.interrupt();
}
}
class Producer implements Runnable{
private BlockingQueue storage;
public Producer(BlockingQueue storage) {
this.storage = storage;
}
@Override
public void run() {
int num = 0;
try{
while(num < Integer.MAX_VALUE / 2 && !Thread.currentThread().isInterrupted()){
if(num % 100 == 0){
this.storage.put(num);
System.out.println(num + "是100的倍數(shù),已經(jīng)被放入倉(cāng)庫(kù)");
}
num++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("生產(chǎn)者停止生產(chǎn)");
}
}
}
class Consumer{
private BlockingQueue storage;
public Consumer(BlockingQueue storage) {
this.storage = storage;
}
public boolean needMoreNums(){
return Math.random() < 0.95 ? true : false;
}
}
4. 擴(kuò)展
可能你還會(huì)問(wèn):如何處理不可中斷的阻塞?
只能說(shuō)很遺憾沒(méi)有一個(gè)通用的解決辦法,我們需要針對(duì)特定的鎖或io給出特定的解決方案。對(duì)于這些特殊的例子,api一般會(huì)給出可以響應(yīng)中斷的操作方法,我們要選用那些特定的方法,沒(méi)有萬(wàn)能藥。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
netty-grpc一次DirectByteBuffer內(nèi)存泄露問(wèn)題
這篇文章主要介紹了netty-grpc一次DirectByteBuffer內(nèi)存泄露問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
jmeter壓力測(cè)試工具簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了jmeter壓力測(cè)試工具相關(guān)介紹資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換
這篇文章主要介紹了java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java Spring boot 2.0 跨域問(wèn)題的解決
本篇文章主要介紹了Java Spring boot 2.0 跨域問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)
今天通過(guò)本文給大家介紹我們應(yīng)該如何使用SpringBoot來(lái)整合spring-retry組件實(shí)現(xiàn)重試機(jī)制及注意事項(xiàng),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
SpringCloud Eureka自我保護(hù)機(jī)制原理解析
這篇文章主要介紹了SpringCloud Eureka自我保護(hù)機(jī)制原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

