A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 鲍霄霄 中级黑马   /  2012-7-18 20:12  /  1773 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


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(p).start();
                new Thread(c).start();
        }
}

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

class SyncStack {
        int index = 0;
        WaWa[] arrWW = new WaWa[6];
       
        public synchronized void push(WaWa ww) {
                while(index == arrWW.length) {
                        try {
                                this.wait();//A处
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                this.notifyAll();        //1处       
                arrWW[index] = ww;
                index ++;
        }
       
        public synchronized WaWa pop() {
                while(index == 0) {
                        try {
                                this.wait();//B处
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                this.notifyAll();//2处
                index--;
                return arrWW[index];
        }
}

class Producer implements Runnable {
        SyncStack ss = null;
        Producer(SyncStack ss) {
                this.ss = ss;
        }
       
        public void run() {
                for(int i=0; i<20; i++) {
                        WaWa ww = new WaWa(i);
                        ss.push(ww);
System.out.println("生产了:" + ww);
                        try {
                                Thread.sleep((int)(Math.random() * 200));
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }                       
                }
        }
}

class Consumer implements Runnable {
        SyncStack ss = null;
        Consumer(SyncStack ss) {
                this.ss = ss;
        }
       
        public void run() {
                for(int i=0; i<20; i++) {
                        WaWa ww = ss.pop();
System.out.println("消费了: " + ww);
                        try {
                                Thread.sleep((int)(Math.random() * 1000));
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }                       
                }
        }
}
麻烦请问一下关于这个程序1处和2处的notify叫醒的分别是哪个wait,最好说一下执行过程谢谢

1H5T}N{M@TA_45GP_FKPR_N.jpg (66.89 KB, 下载次数: 37)

1H5T}N{M@TA_45GP_FKPR_N.jpg

3 个回复

倒序浏览
我觉得你对多线程的理解很模糊   建立楼主回头去看看视频
回复 使用道具 举报
蒋映辉 发表于 2012-7-19 14:45
我觉得你对多线程的理解很模糊   建立楼主回头去看看视频

好的  回头一定好好看
回复 使用道具 举报
notifyAll会唤醒线程池中所有等待的线程,之后他们自由竞争
notify   只会唤醒排在线程池中等待的第一个线程
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马