zoukankan      html  css  js  c++  java
  • classic problem: producer and consumer

    注意: 1. wait()后 立即放锁, 给其他等待线程使用,当被唤醒,立即从wait()处执行(而不是从方法头重新执行)

              2. notify(), 唤醒其他睡着的线程,不能唤醒自己。

              3. 没有结束的问题用 while(用if, 则可能在错误情况下继续执行)。

    public class ProducerConsumer {
        public static void main(String[] args) {
            SyncStack ss = new SyncStack();
           
            Producer p = new Producer(ss);
            Consumer c = new Consumer(ss);
           
            new Thread(c).start();
            new Thread(p).start();
            new Thread(p).start();
            new Thread(p).start();
        }
    }

    class Wotou {
        int id;
       
        Wotou(int id) {
            this.id = id;
        }
       
        public String toString() {
            return "Wotou: " + id;
        }
    }

    class SyncStack {
        int index = 0;
        //Wotou[] wotou = new Wotou[6];
       
        public synchronized void push(/*Wotou wt,*/ int i) {       
            while (index == 6) {
                System.out.println("----push(Wotou wt):" + index);
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            }

            this.notify();
            System.out.println(Thread.currentThread().getName() + "-----producer: " + i );
            //wotou[index] = wt;
            index++;
        }
       
        public synchronized int pop(int i) {
            System.out.println("pop(: " + i);
            while (index == 0) {
                System.out.println("pop():" + index);
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            }

            System.out.println("pre this.notify()");
            this.notify();
            System.out.println("end this.notify()");
           
            index--;
            System.out.println("Consumer: " + i);
            return index;
            //return wotou[index];
        }
    }

    class Producer implements Runnable{
        SyncStack ss;
       
        Producer(SyncStack ss) {
            this.ss = ss;
        }
       
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i=0; i<10; i++){
                Wotou wt = new Wotou(i);
                ss.push(/*wt,*/ i);
                           
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie){
                    ie.printStackTrace();
                }
            }   
        }
    }

    class Consumer implements Runnable{
        SyncStack ss;
       
        Consumer(SyncStack ss) {
            this.ss = ss;
        }
       
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i=0; i<30; i++){
                ss.pop(i);
               
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ie){
                    ie.printStackTrace();
                }
            }   
        }
    }

  • 相关阅读:
    快的打车 技术部 在 杭州 招聘 #年前面试 年后入职#架构师
    王大锤_百度百科
    2013年总结
    泥沙俱下_百度百科
    thank you letter
    上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有
    外省人员-办理护照_百度经验
    敬请贤者:WEB、IOS开发(2年以上经验,大专);CTO、产品经理,运营专员 电商服装鞋饰买手(2年以上经验,服装或鞋类);体验店店长 (2年以上经验,服装或鞋类) 工作地点:丰台南苑路;有意者小窗QQ2211788980
    “快的打车”创始人陈伟星的新项目招人啦,高薪急招Java服务端/Android/Ios 客户端研发工程师/ mysql DBA/ app市场推广专家,欢迎大家加入我们的团队!
    【深圳,武汉】一加科技(One Plus)招聘,寻找不...
  • 原文地址:https://www.cnblogs.com/tiechui/p/1893600.html
Copyright © 2011-2022 走看看