Java生產者與消費者模式的簡單寫法__Java

來源:互聯網
上載者:User

生產者消費者模式是研究多線程程式的經典問題之一,它描述是有一塊緩衝區作為緩衝隊列/倉庫,生產者可以將產品放入隊列,消費者則可以從隊列中取走產品。大多數的後台服務程式並發控制的基本原理都可以歸納為生產者消費者模式。

1、使用Synchronized()、wait() 、 notify()、notifyAll()方法實現:

package proAndCsmModel01;import java.util.LinkedList;/** * 實現緩衝區 *  */public class Resource01 {    //最大緩衝區    private final int MAX_SIZE = 10;    //緩衝區隊列    LinkedList<datatype> list = new LinkedList<>();    /**     * 生產資料同步方法     */    public  synchronized void increaseData(){            while (list.size() >= MAX_SIZE){                try {                    System.out.println(Thread.currentThread().getId()+"資料倉庫已滿!");                    wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            datatype d = new datatype();            d.setData((int) (Math.random()*1000));            list.add(d);            System.out.println(Thread.currentThread().getId()+"生產:"+d.getData()+" 庫存量:"+list.size());            notifyAll();    }    /**     * 消費資料同步方法     */    public synchronized void decreaseData(){            while (list.size() <= 0){                try {                    System.out.println(Thread.currentThread().getId()+"資料倉庫為空白!");                    wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            datatype d = list.poll();            System.out.println(Thread.currentThread().getId()+"消費:"+d.getData()+" 庫存量:"+list.size());            notifyAll();    }}package proAndCsmModel01;/** * 生產者:生產資料 *  */public class producer01 implements Runnable {    private Resource01 resource01;    producer01(Resource01 resource01){        this.resource01 = resource01;    }    @Override    public void run() {        while (true){            try {                //隨機休眠後在生產資料                Thread.sleep((long) (Math.random()*1000));            } catch (InterruptedException e) {                e.printStackTrace();            }            //調用同步方法進行生產資料            resource01.increaseData();        }    }}package proAndCsmModel01;/** * 消費者:消費資料 *  */public class consumer01 implements Runnable {    private Resource01 resource01;    consumer01(Resource01 resource01){        this.resource01 = resource01;    }    @Override    public void run() {         while (true){             try {                 //隨機休眠後再消費                 Thread.sleep((long) (Math.random()*1000));             } catch (InterruptedException e) {                 e.printStackTrace();             }             //調用同步方法進行消費資料             resource01.decreaseData();         }    }}package proAndCsmModel01;/** * 基礎資料型別 (Elementary Data Type) *  */public class datatype {    private int data;    public void setData(int data) {        this.data = data;    }    public int getData() {        return data;    }}package proAndCsmModel01;/** * 調用 * */public class test {    public static void main(String agrs[]){        Resource01 resource01 = new Resource01();;        System.out.println(Thread.currentThread().getName());        new Thread(new producer01(resource01)).start();        new Thread(new producer01(resource01)).start();        new Thread(new producer01(resource01)).start();        new Thread(new producer01(resource01)).start();        new Thread(new producer01(resource01)).start();        new Thread(new producer01(resource01)).start();        new Thread(new consumer01(resource01)).start();        new Thread(new consumer01(resource01)).start();        new Thread(new consumer01(resource01)).start();    }}


2、ReentrantLock、await() 、signal()、signalAll()方法實現:

package exampletest.proAndCsmModel01;import java.util.LinkedList;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 實現緩衝區 *  */public class Resource02 {    //最大緩衝區    private final int MAX_SIZE = 10;    //緩衝區隊列    LinkedList<datatype> list = new LinkedList<>();    //同步鎖和條件    private Lock lock = new ReentrantLock();    private Condition full_cd = lock.newCondition();    private Condition empty_cd = lock.newCondition();    /**     * 生產資料同步方法     */    public void increaseData(){        lock.lock();        while (list.size() >= MAX_SIZE){            try {                System.out.println(Thread.currentThread().getId()+"資料倉庫已滿!");                full_cd.await();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        datatype d = new datatype();        d.setData((int) (Math.random()*1000));        list.add(d);        System.out.println(Thread.currentThread().getId()+"生產:"+d.getData()+" 庫存量:"+list.size());        empty_cd.signalAll();        lock.unlock();    }    /**     * 消費資料同步方法     */    public void decreaseData(){        lock.lock();        while (list.size() <= 0){            try {                System.out.println(Thread.currentThread().getId()+"資料倉庫為空白!");                empty_cd.await();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        datatype d = list.poll();        System.out.println(Thread.currentThread().getId()+"消費:"+d.getData()+" 庫存量:"+list.size());        full_cd.signalAll();        lock.unlock();    }}

3、使用BlockingQueue阻塞隊列方法實現
package exampletest.proAndCsmModel01;import java.util.LinkedList;import java.util.concurrent.LinkedBlockingDeque;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 實現緩衝區 *  */public class Resource03 extends Resource{    //最大緩衝區    private final int MAX_SIZE = 10;    //緩衝區隊列    LinkedBlockingDeque<datatype> list = new LinkedBlockingDeque<>(MAX_SIZE);    /**     * 生產資料同步方法     */    @Override    public void increaseData(){        if (list.size() >= MAX_SIZE){            System.out.println(Thread.currentThread().getId()+"資料倉庫已滿!");        }        datatype d = new datatype();        d.setData((int) (Math.random()*1000));        try {            list.put(d);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getId()+"生產:"+d.getData()+" 庫存量:"+list.size());    }    /**     * 消費資料同步方法     */    @Override    public void decreaseData(){        if (list.size() <= 0){            System.out.println(Thread.currentThread().getId()+"資料倉庫為空白!");        }        datatype d = null;        try {            d = list.take();        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getId()+"消費:"+d.getData()+" 庫存量:"+list.size());    }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.