1. 程式人生 > 實用技巧 >11.redis 五大資料型別及常用命令

11.redis 五大資料型別及常用命令

執行緒終止

學習材料來源於網路
如有侵權,聯絡刪除

如何終止一個執行緒

stop()停止執行緒

Thead執行緒

package icu.shaoyayu.multithreading.chapter1;

/**
 * @author shaoyayu
 * @E_Mail
 * @Version 1.0.0
 * @readme :
 */
public class StopThread extends Thread {
    private int i = 0, j = 0;

    @Override
    public void run() {
        synchronized (this) {
            // 增加同步鎖,確保執行緒安全
            ++i;
            try {
                // 休眠10秒,模擬耗時操作
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ++j;
        }
    }

    /** * 列印i和j */
    public void print() {
        System.out.println("i=" + i + " j=" + j);
    }
}

stop()方法停止方式

package icu.shaoyayu.multithreading.chapter1;

/**
 * @author shaoyayu
 * @E_Mail
 * @Version 1.0.0
 * @readme :
 */
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        StopThread thread = new StopThread();
        thread.start();
        // 休眠1秒,確保i變數自增成功
        Thread.sleep(1000);
        // 暫停執行緒
        thread.stop(); // 錯誤的終止
        // thread.interrupt(); // 正確終止
        while (thread.isAlive()) {
            // 確保執行緒已經終止
        } // 輸出結果
        thread.print();
    }
}

這種停止方式最大的問題就是,線上程執行synchronized裡面的任務的時候,執行緒終止的時候,會直接的吧執行緒結束,不能保證原子性


interrupt() 停止執行緒

正確的執行緒中止-interrupt

如果目標執行緒在呼叫Object class的wait()、wait(long)或wait(long,int)方法、join()、join(long, int)或sleep(long, int)方法時被阻塞,那麼Interrupt會生效,該執行緒的中斷狀態將被清除,丟擲InterruptedException異常。
如果目標執行緒是被I/О或者NIO中的Channel所阻塞,同樣,I/O操作會被中斷或者返回特殊異常值。達到終止執行緒的目的。
如果以上條件都不滿足,則會設定此執行緒的中斷狀態。

package icu.shaoyayu.multithreading.chapter1;

/**
 * @author shaoyayu
 * @E_Mail
 * @Version 1.0.0
 * @readme :
 */
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        StopThread thread = new StopThread();
        thread.start();
        // 休眠1秒,確保i變數自增成功
        Thread.sleep(1000);
        // 暫停執行緒
        thread.interrupt(); // 正確終止
        while (thread.isAlive()) {
            // 確保執行緒已經終止
        } // 輸出結果
        thread.print();
    }
}

執行緒被終止的時候,會丟擲InterruptedException異常,可以在這裡做相對於的處理

    @Override
    public void run() {
        synchronized (this) {
            // 增加同步鎖,確保執行緒安全
            ++i;
            try {
                // 休眠10秒,模擬耗時操作
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ++j;
        }
    }

標誌位停止執行緒

使用標誌位停止執行緒,主要停止的執行緒是迴圈任務的執行緒

package icu.shaoyayu.multithreading.chapter1;

/**
 * @author shaoyayu
 * @E_Mail
 * @Version 1.0.0
 * @readme :
 */
public class Demo4 extends Thread {
    public volatile static boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                while (flag) { // 判斷是否執行
                    System.out.println("執行中");
                    Thread.sleep(1000L);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        // 3秒之後,將狀態標誌改為False,代表不繼續執行
        Thread.sleep(3000L);
        flag = false;
        System.out.println("程式執行結束");
    }
}