JAVA學習筆記(四十二)-生產者消費者模型

來源:互聯網
上載者:User

標籤:wait   生產者消費者   

wait()、notify()
/* * wait()、notify() *  * 1.兩個方法都只能在synchronized代碼塊中執行,因為要對持有鎖的線程操作,只有同步中才有鎖 * 2.兩個方法在操作同步中的線程時,必須要標識所操作線程持有的對象鎖 * 3.等待和喚醒必須是同一個對象鎖 */public class Test05 {    public static void main(String[] args) {        MyThread3 mt=new MyThread3();        Thread th=new Thread(mt,"線程一");        th.start();        try {            Thread.sleep(2000);//主線程休眠2秒,讓線程th在等待池中待一會        } catch (InterruptedException e) {            e.printStackTrace();        }        //打斷處於mt對象的等待池中的線程th        //th.interrupt();        synchronized (mt) {            mt.notify();//喚醒對象mt的等待池中的一個線程        }    }}class MyThread3 implements Runnable{    @Override    public void run() {        //同步代碼塊        synchronized (this) {            System.out.println("***進入同步代碼塊");            try {                wait();//線程進入wait池,處於阻塞狀態                System.out.println("***wait之後重新拿到了鎖");            } catch (InterruptedException e) {                System.out.println("***"+Thread.currentThread().getName()+"被打斷!");            }            for(int i=1;i<=20;i++){                System.out.println(Thread.currentThread().getName()+"***"+i);            }            System.out.println("***同步代碼塊執行結束!");        }    }}
生產者、消費者模型
/* * 線程間通訊:多個線程操作同一個資源,但操作的動作不同 * 生產者、消費者模型 * wait()和notify()方法一般應用於生產者和消費者模型中,用來等待同步資料 */public class Test06 {    public static void main(String[] args) {        Person person = new Person();        Input in = new Input(person);        Output out = new Output(person);        Thread th1 = new Thread(in,"生產者生產了: ");// 產生人        Thread th2 = new Thread(out,"消費者消費了: ");// 取出人        th1.start();        th2.start();    }}class Person {    String name;    String sex;    boolean flag = true;// true表示需要產生人,false表示取出人}// 生產、入class Input implements Runnable {    private Person person;    public Input(Person person) {        this.person = person;    }    @Override    public void run() {        int x = 0;        while (true) {            synchronized (person) {                if (person.flag) {                    if (x == 0) {// 判斷間隔產生不同的人                        person.name = "tom";                        person.sex = "male";                    } else {                        person.name = "張三";                        person.sex = "女";                    }                    x = (x + 1) % 2; // 間隔產生不同的人                    person.flag = false;// 放一次,取一次                    person.notify();//喚醒取人的線程                } else {                    try {                        person.wait();// 如果不需要產生人,則等待                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }    }}// 消費、出class Output implements Runnable {    private Person person;    public Output(Person person) {        this.person = person;    }    @Override    public void run() {        while (true) {            synchronized (person) {                if (!person.flag) {                    System.out.println(Thread.currentThread().getName()+person.name + "***" + person.sex);                    person.flag = true;                    person.notify();//喚醒生產人的線程                } else {                    try {                        person.wait();//如果不需要取出人,則等待                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }    }}

JAVA學習筆記(四十二)-生產者消費者模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.