停止線程和守護線程(傳智播客)

一.停止線程
1.標記停止線程
原有的stop和suspend方法存在固有的安全性問題,尤其是suspend有死鎖傾向。

public class StopThread implements Runnable{
    private boolean flag = true;
    @Override
    public void run() {
        while(flag){
            System.out.println(Thread.currentThread()+"...run...");
        }
    }

    public void changeFlag(){
        this.flag = false;
    }

    public static void main(String[] args) {
        StopThread st = new StopThread();
        Thread t0 = new Thread(st);
        Thread t1 = new Thread(st);
        t0.start();
        t1.start();
        int num = 1;
        for(;;){
            if(++num==50){
                st.changeFlag();
                break;
            }
            System.out.println("main..."+num);
        }
        System.out.println("over");
    }
}

在代碼中讓線程進入凍結狀態,這種方法有停不下來的時候。

2.interrupt方法結束線程
可以使用interrupt方法將線程從凍結狀態強制恢復到運行狀態,讓線程具備cpu執行資格。但是這種操作是強制性的,會造成InterruptedException,需要進行處理,如在本例中的讀取標記。

public class StopThread implements Runnable{
    private boolean flag = true;
    @Override
    public synchronized void run() {
        while(flag){
            try{
                wait();
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread()+e.toString());
                flag = false;
            }
            System.out.println(Thread.currentThread()+"...run...");
        }
    }

    public void changeFlag(){
        this.flag = false;
    }

    public static void main(String[] args) {
        StopThread st = new StopThread();
        Thread t0 = new Thread(st);
        Thread t1 = new Thread(st);
        t0.start();
        t1.start();
        int num = 1;
        for(;;){
            if(++num==50){
                t0.interrupt();
                t1.interrupt();
                break;
            }
            System.out.println("main..."+num);
        }
        System.out.println("over");
    }
}

二.守護線程
守護線程又稱爲用戶線程或者後臺線程,它的創建和運行狀態和其它線程一樣,但是隻要前臺線程(系統線程)全部結束,那麼無論守護線程處於何種狀態都自動結束。當一個正在運行的java程序內所有的線程都爲守護線程時,jvm退出。

public class StopThread implements Runnable{
    private boolean flag = true;
    @Override
    public synchronized void run() {
        while(flag){
            try{
                wait();
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread()+e.toString());
                flag = false;
            }
            System.out.println(Thread.currentThread()+"...run...");
        }
    }

    public void changeFlag(){
        this.flag = false;
    }

    public static void main(String[] args) {
        StopThread st = new StopThread();
        Thread t0 = new Thread(st);
        Thread t1 = new Thread(st);
        t0.start();
        t1.setDaemon(true);
        t1.start();
        int num = 1;
        for(;;){
            if(++num==50){
                t0.interrupt();
                break;
            }
            System.out.println("main..."+num);
        }
        System.out.println("over");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章