我思故我強(qiáng)

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

           

          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)滿了 等待消費(fèi)
             System.out.println("已經(jīng)存滿!等待消費(fèi)。。。。。");
             try {
              this.wait();
             } catch (InterruptedException e) {
              e.printStackTrace();
             }
            }
            this.notify();
            things[counter] = thing;
            counter++;
           }

           public synchronized Thing pop() {
            // 消費(fèi)
            while (counter == 0) {
             System.out.println("沒(méi)有產(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;// 消費(fèi)水平

           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("消費(fèi): " + 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);
            // 存儲(chǔ)量為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) 評(píng)論(0)  編輯  收藏 所屬分類: 面試筆試相關(guān)的

          主站蜘蛛池模板: 彩票| 抚州市| 长泰县| 吉木乃县| 永寿县| 福建省| 陆良县| 随州市| 法库县| 普兰店市| 宜州市| 高雄市| 西乌珠穆沁旗| 都匀市| 靖西县| 金沙县| 雷州市| 淮安市| 漠河县| 黄大仙区| 彰化县| 志丹县| 宁明县| 鄂托克前旗| 榆树市| 个旧市| 南康市| 达孜县| 沈阳市| 云浮市| 辽源市| 安陆市| 浮山县| 荆州市| 山东| 阜城县| 吴江市| 邵武市| 吉水县| 垦利县| 固始县|