<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java世界

    學習筆記

    常用鏈接

    統計

    積分與排名

    天籟村

    新華網

    雅虎

    最新評論

    多線程案例:生產者和消費者

    1.生產和消費的產品抽象類:
    public abstract class Product {
        public String name;
        public abstract String toString();
    }

    2.一個具體的產品類:

    public class AProduct extends Product {
        public AProduct(String name) {
            this.name = name;
            // TODO Auto-generated constructor stub
        }
        public String toString() {
            // TODO Auto-generated method stub
            return this.name;
        }
    }

    3.容器類(倉庫):

    import java.util.ArrayList;

    /*
     * 存放生產者和消費者的產品隊列
     * */

    public class Container {
        private ArrayList arrList = new ArrayList();
        private int LENGTH = 10;
        public boolean isFull() {
            return arrList.size()==LENGTH;
        }
        public boolean isEmpty() {
            return arrList.isEmpty();
        }

        /* 如果此處不加synchronized鎖,那么也可以再調用push的地方加鎖
        * 既然此處加了鎖,那么再別的地方可以不加鎖
        */

        public synchronized void push(Object o) {
            arrList.add(o);
        }
        // 如果此處不加synchronized鎖,那么也可以再調用push的地方加鎖
        public synchronized Object pop() {
            Object lastOne = arrList.get(arrList.size()- 1);
            arrList.remove(arrList.size()- 1);
            return lastOne;
        }
    }

    4.休息一會,生產者和消費者都要休息,因此作為抽象基類:

    public abstract class Sleep {
        public void haveASleep() throws InterruptedException {
            Thread.sleep((long)(Math.random()* 3000));
        }
    }

    /*
     * 消費者線程
     * */

    public class Consumer extends Sleep implements Runnable {
        private Container contain =null;
        public Consumer(Container contain) {
            this.contain = contain;
        }
        public void run() {
            // TODO Auto-generated method stub
            while(true) {
                synchronized(contain) {
                    while(contain.isEmpty()) {
                        try{
                            contain.wait();
                        }catch(InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                consume();//消費
                try {
                    haveASleep();
                }catch(InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                synchronized(contain) {
                    contain.notify();
                }
            }
        }
        private void consume() {
            Product a = (AProduct)contain.pop();
            System.out.println("消費了一個產品"+ a.toString());
        }
    }

    /*
     * 生產者線程
     * */

    public class Producator extends Sleep implements Runnable {
        private Container contain = null;
        public Producator(Container contain) {
            super();
            this.contain = contain;
        }
        public void run() {
            // TODO Auto-generated method stub
            while(true) {
                synchronized(contain) {
                    while(contain.isFull()) {
                        try{
                            contain.wait();// 阻塞當前線程,當前線程進入等待隊列。這個時候只有等待別的線程來喚醒自己了。
                        }catch(InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                producer();// 生產一個產品
                try {
                    haveASleep();
                }catch(InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                synchronized(contain) {
                    contain.notify();// 喚醒等待隊列中正在等待的第一個線程,讓其執行。
                }
            }
        }
        public void producer() {
            Product aProduct = new AProduct("pp:"+String.valueOf((int)(10*Math.random())));
            System.out.println("生產了一個產品:"+ aProduct.toString());
            contain.push(aProduct);
        }
    }

    5. 寫一個測試:

    public class TestMain {
        /**
         * @param args
         */

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Container contain = new Container();
            Producator p = new Producator(contain);
            Consumer c = new Consumer(contain);
            Thread pt =new Thread(p);
            Thread ct =new Thread(c);
            pt.start();
            ct.start();
        }
    }

    posted on 2007-11-06 09:39 Rabbit 閱讀(1976) 評論(1)  編輯  收藏

    評論

    # re: 多線程案例:生產者和消費者 2007-12-14 01:30 殤傷吾雁

    兄弟,轉載的吧

    轉載請注明  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产伦精品一区二区三区免费迷 | 亚洲精品第一国产综合精品99| 精品亚洲一区二区| www免费黄色网| 一本色道久久综合亚洲精品高清| 国产亚洲精品美女2020久久| 在线观看永久免费视频网站| 亚洲AV无码AV日韩AV网站| 特级淫片国产免费高清视频| 亚洲国产一区二区三区在线观看 | 免费观看国产网址你懂的| 亚洲美女免费视频| 曰批视频免费30分钟成人| 国产成人亚洲综合网站不卡| 操美女视频免费网站| 亚洲精品无码成人| 亚洲AV无码乱码在线观看性色扶| 一区二区三区视频免费观看| 亚洲中文字幕无码爆乳AV| 人妻丰满熟妇无码区免费| 亚洲一线产区二线产区精华| 女人18特级一级毛片免费视频| jzzijzzij在线观看亚洲熟妇| 免费人成网站在线高清| 成年女人A毛片免费视频| 久久91亚洲人成电影网站| 久久九九兔免费精品6| 亚洲av永久无码精品秋霞电影秋 | 亚洲视频免费在线播放| 无人在线观看免费高清视频| 国产亚洲视频在线播放大全| 亚洲色中文字幕无码AV| 国产成人精品免费视频网页大全| 亚洲熟妇无码八V在线播放| 亚洲欧洲精品成人久久曰影片| 无码少妇精品一区二区免费动态| 亚洲男人天堂2022| 伊人久久亚洲综合| 野花高清在线观看免费3中文| 人妻免费久久久久久久了| 亚洲视频免费在线看|