我思故我強(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("沒有產(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);
            // 存儲量為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 李云澤 閱讀(146) 評論(0)  編輯  收藏 所屬分類: 面試筆試相關(guān)的

          主站蜘蛛池模板: 郧西县| 汉川市| 平顺县| 北碚区| 万全县| 蓬莱市| 湟中县| 怀柔区| 卢湾区| 彭州市| 昔阳县| 浦东新区| 神农架林区| 兰州市| 邮箱| 同仁县| 墨竹工卡县| 抚顺市| 东乌珠穆沁旗| 张家港市| 襄汾县| 甘孜县| 安义县| 鲜城| 洛隆县| 新田县| 高阳县| 淳化县| 调兵山市| 山东| 宜都市| 淮南市| 龙门县| 会宁县| 岳池县| 元阳县| 洪洞县| 沧州市| 阿城市| 吉水县| 遂昌县|