多線程之間實現通信(生產者與消費者)

多線程之間通信實現(並解決線程安全問題)

class Res {
    public String userName;
    public String sex;
}

class InputThread extends Thread {
    private Res res;

    public InputThread(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
             synchronized (res) {
            if (count == 0) {
                res.userName = "餘勝軍";
                res.sex = "男";
            } else {
                res.userName = "小紅";
                res.sex = "女";
            }
            count = (count + 1) % 2;
        }

        }
    }
}

class OutThrad extends Thread {
    private Res res;

    public OutThrad(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (res) {
                System.out.println(res.userName + "," + res.sex);
            }
        }

    }
}

public class ThreadDemo01 {

    public static void main(String[] args) {
        Res res = new Res();
        InputThread inputThread = new InputThread(res);
        OutThrad outThrad = new OutThrad(res);
        inputThread.start();
        outThrad.start();
    }

}

這種方式的問題在於,會進行屢次消費java

wait、notify方法

多線程之間通信,其實就是多個線程在操做同一個資源,可是操做的動做不一樣。安全

  1. 由於涉及到對象鎖,他們必須都放在synchronized中來使用. Wait、Notify必定要在synchronized裏面進行使用。
  2. Wait必須暫定當前正在執行的線程,並釋放資源鎖,讓其餘線程能夠有機會運行
  3. notify/notifyall: 喚醒因鎖池中的線程,使之運行

注意:必定要在線程同步中使用,而且是同一個鎖的資源多線程

class Res {
    public String userSex;
    public String userName;
    //線程通信標識
    public boolean flag = false;
}
class IntThrad extends Thread {
    private Res res;

    public IntThrad(Res res) {
        this.res = res;        
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (res) {
                if (res.flag) {
                    try {
                       // 當前線程變爲等待,可是能夠釋放鎖
                        res.wait();
                    } catch (Exception e) {

                    }
                }
                if (count == 0) {
                    res.userName = "小軍";
                    res.userSex = "男";
                } else {
                    res.userName = "小紅";
                    res.userSex = "女";
                }
                count = (count + 1) % 2;
                res.flag = true;
                // 喚醒當前線程
                res.notify();
            }

        }
    }
}
class OutThread extends Thread {
    private Res res;

    public OutThread(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (res) {
                if (!res.flag) {
                    try {
                        res.wait();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                System.out.println(res.userName + "--" + res.userSex);
                res.flag = false;
                res.notify();
            }
        }
    }
}

public class ThreaCommun {
    public static void main(String[] args) {
        Res res = new Res();
        IntThrad intThrad = new IntThrad(res);
        OutThread outThread = new OutThread(res);
        intThrad.start();
        outThread.start();
    }
}

wait與sleep區別

對於sleep()方法,咱們首先要知道該方法是屬於Thread類中的。而wait()方法,則是屬於Object類中的。併發

sleep()方法致使了程序暫停執行指定的時間,讓出cpu該其餘線程,可是他的監控狀態依然保持者,當指定的時間到了又會自動恢復運行狀態。ide

在調用sleep()方法的過程當中,線程不會釋放對象鎖。this

而當調用wait()方法的時候,線程會放棄對象鎖,進入等待此對象的等待鎖定池,只有針對此對象調用notify()方法後本線程才進入對象鎖定池準備線程

獲取對象鎖進入運行狀態。code

Lock鎖

在 jdk1.5 以後,併發包中新增了 Lock 接口(以及相關實現類)用來實現鎖功能,Lock 接口提供了與 synchronized 關鍵字相似的同步功能,但須要在使用時手動獲取鎖和釋放鎖。對象

Lock寫法

Lock lock  = new ReentrantLock();
lock.lock();
try{
//可能會出現線程安全的操做
}finally{
//必定在finally中釋放鎖
//也不能把獲取鎖在try中進行,由於有可能在獲取鎖的時候拋出異常
  lock.ublock();
}

Lock與synchronized 關鍵字的區別

  • Lock 接口能夠嘗試非阻塞地獲取鎖 當前線程嘗試獲取鎖。若是這一時刻鎖沒有被其餘線程獲取到,則成功獲取並持有鎖。
  • Lock 接口能被中斷地獲取鎖 與 synchronized 不一樣,獲取到鎖的線程可以響應中斷,當獲取到的鎖的線程被中斷時,中斷異常將會被拋出,同時鎖會被釋放。
  • Lock 接口在指定的截止時間以前獲取鎖,若是截止時間到了依舊沒法獲取鎖,則返回。

Condition用法

Condition的功能相似於在傳統的線程技術中的,Object.wait()和Object.notify()的功能。
代碼接口

Condition condition = lock.newCondition();
res. condition.await();  相似wait
res. Condition. Signal() 相似notify
class Res {
    public String userName;
    public String sex;
    public boolean flag = false;
    Lock lock = new ReentrantLock();
}

class InputThread extends Thread {
    private Res res;
    Condition newCondition;
    public InputThread(Res res,    Condition newCondition) {
        this.res = res;
        this.newCondition=newCondition;
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            // synchronized (res) {

            try {
                res.lock.lock();
                if (res.flag) {
                    try {
//                        res.wait();
                        newCondition.await();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                if (count == 0) {
                    res.userName = "小軍";
                    res.sex = "男";
                } else {
                    res.userName = "小紅";
                    res.sex = "女";
                }
                count = (count + 1) % 2;
                res.flag = true;
//                res.notify();
                newCondition.signal();
            } catch (Exception e) {
                // TODO: handle exception
            }finally {
                res.lock.unlock();
            }
        }

        // }
    }
}

class OutThrad extends Thread {
    private Res res;
    private Condition newCondition;
    public OutThrad(Res res,Condition newCondition) {
        this.res = res;
        this.newCondition=newCondition;
    }

    @Override
    public void run() {
        while (true) {
//            synchronized (res) {
            try {
                res.lock.lock();
                if (!res.flag) {
                    try {
//                        res.wait();
                        newCondition.await();
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                System.out.println(res.userName + "," + res.sex);
                res.flag = false;
//                res.notify();
                newCondition.signal();
            } catch (Exception e) {
                // TODO: handle exception
            }finally {
                res.lock.unlock();
            }
//            }
        }

    }
}

public class ThreadDemo01 {

    public static void main(String[] args) {
        Res res = new Res();
        Condition newCondition = res.lock.newCondition();
        InputThread inputThread = new InputThread(res,newCondition);
        OutThrad outThrad = new OutThrad(res,newCondition);
        inputThread.start();
        outThrad.start();
    }

}
本文由博客一文多發平臺 OpenWrite 發佈!
相關文章
相關標籤/搜索