我思故我強

          多線程-生產(chǎn)者消費者

           

          package test;

          class Thing {
           int id;

           public Thing(int id) {
            this.id = id;
           }
          }

          class Stack {
           int counter = 0;
           int stackSize;
           Thing[] things = null;

           public Stack(int stackSize) {
            this.stackSize = stackSize;
            things = new Thing[stackSize];
           }

           public synchronized void push(Thing thing) {
            // 生產(chǎn)
            while (counter == stackSize) {
             // 已經(jīng)滿了 等待消費
             System.out.println("已經(jīng)存滿!等待消費。。。。。");
             try {
              this.wait();
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            }
            this.notify();
            things[counter] = thing;
            counter++;
           }

           public synchronized Thing pop() {
            // 消費
            while (counter == 0) {
             System.out.println("沒有產(chǎn)品!等待生產(chǎn)。。。。");
             try {
              this.wait();
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            }
            this.notify();
            counter--;
            return things[counter];
           }
          }

          class Producer implements Runnable {
           Stack stack;
           int level;// 生產(chǎn)水平

           public Producer(Stack stack, int level) {
            this.stack = stack;
            this.level = level;
           }

           public void run() {
            for (int i = 0; i <= level; i++) {
             Thing thing = new Thing(i);
             stack.push(thing);
             System.out.println("生產(chǎn): " + thing.id);
             try {
              Thread.sleep(100);
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            }
           }
          }

          class Consumer implements Runnable {
           Stack stack;
           int level;// 消費水平

           public Consumer(Stack stack, int level) {
            this.stack = stack;
            this.level = level;
           }

           public void run() {
            for (int i = 0; i <= level; i++) {
             Thing thing = stack.pop();
             System.out.println("消費: " + thing.id);
             try {
              Thread.sleep(800);
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            }
           }
          }

          public class ProducterConsumer {
           public static void main(String[] args) {
            int stackSize = 5;

            Stack stack = new Stack(stackSize);
            // 存儲量為5的容器
            Producer p = new Producer(stack, 10);
            Consumer c = new Consumer(stack, 10);
            new Thread(p).start();
            new Thread(c).start();
           }
          }

          posted on 2009-10-12 17:31 李云澤 閱讀(143) 評論(0)  編輯  收藏 所屬分類: 面試筆試相關(guān)的

          主站蜘蛛池模板: 民乐县| 高邮市| 周至县| 寿光市| 防城港市| 永春县| 石棉县| 桃园市| 怀宁县| 新津县| 天镇县| 阳春市| 腾冲县| 藁城市| 芦溪县| 泰顺县| 康平县| 闽清县| 无极县| 南木林县| 亚东县| 安陆市| 丹阳市| 凤山市| 成都市| 靖江市| 宕昌县| 舞阳县| 张家川| 来宾市| 阿荣旗| 嵩明县| 乌兰察布市| 厦门市| 陆良县| 凉山| 霞浦县| 从化市| 策勒县| 铁岭市| 南丰县|