国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

詳解Java停止線(xiàn)程的四種方法

發(fā)布時(shí)間:2021-07-05 18:40 來(lái)源:腳本之家 閱讀:0 作者:fanrendale 欄目: 開(kāi)發(fā)技術(shù)

一、線(xiàn)程停止基礎知識

  • interrupted(): 測試當前線(xiàn)程是否已經(jīng)中斷。該方法為靜態(tài)方法,調用后會(huì )返回boolean值。不過(guò)調用之后會(huì )改變線(xiàn)程的狀態(tài),如果是中斷狀態(tài)調用的,調用之后會(huì )清除線(xiàn)程的中斷狀態(tài)。
  • isInterrupted(): 測試線(xiàn)程是否已經(jīng)中斷。該方法由對象調用
  • interrupt(): 標記線(xiàn)程為中斷狀態(tài),不過(guò)不會(huì )中斷正在運行的線(xiàn)程。
  • stop(): 暴力停止線(xiàn)程。已棄用。

二、停止線(xiàn)程方法1:異常法停止

   線(xiàn)程調用interrupt()方法后,在線(xiàn)程的run方法中判斷當前對象的interrupted()狀態(tài),如果是中斷狀態(tài)則拋出異常,達到中斷線(xiàn)程的效果。

   如下示例:

MyThread.java

public class MyThread extends Thread {

    @Override
    public void run() {
        try {
            for (int i = 0; i < 500000; i++) {
                if (MyThread.interrupted()){
                    System.out.println("已經(jīng)是停止狀態(tài)了,我要退出了!");
                    throw new InterruptedException();
                }
                System.out.println("i = " + (i+1));
            }

            System.out.println("如果我被輸出了,則代表線(xiàn)程沒(méi)有停止");
        } catch (InterruptedException e) {
            System.out.println("在MyThread類(lèi)中的run方法中被捕獲");
            e.printStackTrace();
        }
    }
}

Main.java

/**
 * 根據中斷狀態(tài)退出for循環(huán)
 * @Author: xjf
 * @Date: 2019/5/25 13:27
 */
public class Main {

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

        System.out.println("end!");
    }
}

結果如下:
i = 19115
i = 19116
i = 19117
i = 19118
i = 19119
end!

已經(jīng)是停止狀態(tài)了,我要退出了!
在MyThread類(lèi)中的run方法中被捕獲
java.lang.InterruptedException
 at com.book.interrupt_exit.MyThread.run(MyThread.java:15)

Process finished with exit code 0

三、停止線(xiàn)程方法2:在沉睡中停止

先將線(xiàn)程sleep,然后調用interrupt標記中斷狀態(tài),interrupt會(huì )將阻塞狀態(tài)的線(xiàn)程中斷。會(huì )拋出中斷異常,達到停止線(xiàn)程的效果。如下示例:

MyThread.java

public class MyThread extends Thread {

    @Override
    public void run() {
        try {
            System.out.println("run-----------start");
            Thread.sleep(5000);
            System.out.println("run-----------end");
        } catch (InterruptedException e) {
            System.out.println("在沉睡中被停止!進(jìn)入catch,線(xiàn)程的是否處于停止狀態(tài):" + this.isInterrupted());
            e.printStackTrace();
        }

    }
}

Main.java

public class Main {

    public static void main(String[] args) {
        try {
            MyThread myThread = new MyThread();
            myThread.start();
            Thread.sleep(2000);
            System.out.println("狀態(tài):"+MyThread.interrupted());
            myThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

結果
run-----------start
狀態(tài):false
java.lang.InterruptedException: sleep interrupted
在沉睡中被停止!進(jìn)入catch,線(xiàn)程的是否處于停止狀態(tài):false
 at java.lang.Thread.sleep(Native Method)
 at com.book.sleep_interrupt.MyThread.run(MyThread.java:13)

線(xiàn)程先調用interrupt標記中斷狀態(tài),然后線(xiàn)程再睡眠。會(huì )拋出中斷異常,達到停止線(xiàn)程的效果。如下:

MyThread1.java

public class MyThread1 extends Thread {

    @Override
    public void run() {
        try {

            for (int i = 0; i < 100000; i++) {
                System.out.println("i = " + (i+1));
            }
            System.out.println("run begin");
            //interrupt是做一個(gè)中斷標記,當時(shí)不會(huì )去中斷正在運行的線(xiàn)程,當該線(xiàn)程處于阻塞狀態(tài)時(shí)就會(huì )進(jìn)行中斷
            //因此,先進(jìn)行interrupt后,再遇到sleep阻塞時(shí),才會(huì )進(jìn)行中斷
            Thread.sleep(200000);
            System.out.println("run end");

        } catch (InterruptedException e) {
            System.out.println("先停止,再遇到了sleep! 進(jìn)入catch!");
            e.printStackTrace();
        }
    }
}

Main1.java

public class Main1 {

    public static void main(String[] args) {

        MyThread1 myThread1 = new MyThread1();
        myThread1.start();
        myThread1.interrupt();
        System.out.println("end!");
    }
}

結果:
i = 99993
i = 99994
i = 99995
i = 99996
i = 99997
i = 99998
i = 99999
i = 100000
run begin

先停止,再遇到了sleep! 進(jìn)入catch!
java.lang.InterruptedException: sleep interrupted
 at java.lang.Thread.sleep(Native Method)
 at com.book.sleep_interrupt.MyThread1.run(MyThread1.java:19)

四、停止線(xiàn)程方法3:stop()暴力停止

線(xiàn)程調用stop()方法會(huì )被暴力停止,方法已棄用。該方法會(huì )有不好的后果:

  1. 強制讓線(xiàn)程停止有可能使一些請理性的工作得不到完成。
  2. 對鎖定的對象進(jìn)行了“解鎖”,導致數據得不到同步的處理,出現數據不一致的問(wèn)題(比如一個(gè)方法加上了synchronized,并在其中進(jìn)行了一個(gè)長(cháng)時(shí)間的處理,而在處理結束之前該線(xiàn)程進(jìn)行了stop(),則未完成的數據將沒(méi)有進(jìn)行到同步的處理)

五、停止線(xiàn)程方法4:使用return停止線(xiàn)程

調用interrupt標記為中斷狀態(tài)后,在run方法中判斷當前線(xiàn)程狀態(tài),如果為中斷狀態(tài)則return,能達到停止線(xiàn)程的效果。

備注:建議使用“拋異?!钡姆椒▉?lái)實(shí)現線(xiàn)程的停止,因為在catch塊中還可以將異常向上拋,使線(xiàn)程停止的事件得以傳播

參考:《Java多線(xiàn)程編程核心技術(shù)》

到此這篇關(guān)于詳解Java停止線(xiàn)程的四種方法的文章就介紹到這了,更多相關(guān)Java停止線(xiàn)程內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

少妇人妻无码专区视频| 精品国产一区二区三区久久影院| 国产亚洲精品视觉盛宴| 国产精品久久久久久无码五月| 久久精品国产精品| 国产精品嫩草影院一二三区入口|