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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王德升 中级黑马   /  2012-6-22 09:02  /  1137 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王德升 于 2012-6-22 13:25 编辑

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

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

class SyncStack {
        int index = 0;
        WoTou[] arrWT = new WoTou[6];//1.这里为啥要这么定义?
        
        public synchronized void push(WoTou wt) {
                while(index == arrWT.length) {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                this.notifyAll();               
                arrWT[index] = wt;
                index ++;
        }
        
        public synchronized WoTou pop() {
                while(index == 0) {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                this.notifyAll();
                index--;// 2.这里为什么要减减呢?
                return arrWT[index];
        }
}

class Producer implements Runnable {
        SyncStack ss = null;//3.为什么要定义成null?
        Producer(SyncStack ss) {
                this.ss = ss;
        }
        
        public void run() {
                for(int i=0; i<20; i++) {
                        WoTou wt = new WoTou(i);
                        ss.push(wt);
System.out.println("生产了:" + wt);
                        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++) {
                        WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
                        try {
                                Thread.sleep((int)(Math.random() * 1000));
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }                        
                }
        }
}

//三个小问题谢谢了。


public class Test{
    public static void main(String args[]) {  
        int[][] m = {{1,2,3},{2,3,4},{4,5,6}};//1.这里定义的事什么数组?
        int[][] n = {{3,4,5},{5,6,7}};
        int[][] mn = new int[3][];
        for(int i = 0; i<m.length;i++) {
            mn = new int[n[0].length];
        }
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < n[0].length; j++) {
                for (int k = 0; k < n.length; k++) {
                    mn[j] += m[k] * n[k][j];//2 这个根本看不懂。
                }
            }
        }
        for(int i = 0; i<mn.length;i++){
            for(int j =0;j<mn.length;j++){
                System.out.print(mn[j]);
                if(j<mn.length-1) System.out.print(",");
            }
            System.out.println();
        }
    }
}

// 两个问题。

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
你这个问问题的方式。。。既然你问的这么简单,我简单给你回答一些
1.WoTou[] arrWT = new WoTou[6]语法有错,估计编译不能通过
2.知道栈么,它这个代码是自定义一个同步的栈,index是栈顶指针,压栈,栈顶指针++,出栈,栈顶指针当然要--
3.这不是什么定义成null,它就是定义了一个 SyncStack类型的引用作为它的成员变量,因为以后这个类要对这个变量操作
---------------
1.二维数组
2.明白矩阵的乘积是怎么来的么?这个明白了估计就能看懂2了。
回复 使用道具 举报
闾丘日月 发表于 2012-6-22 13:12
你这个问问题的方式。。。既然你问的这么简单,我简单给你回答一些
1.WoTou[] arrWT = new WoTou[6]语法有 ...

怎么可能你复制去编译下,我是在其他视频上看的。
回复 使用道具 举报
本帖最后由 闾丘日月 于 2012-6-22 13:50 编辑

是我看错了,这行代码定义了WoTou类型的长度为6的数组。
这段代码其实就是给栈开辟一块内存空间,让生产者生产出来的东西有地方放。
说实话,你在哪个视频看到的WoTou这个定义看着真是不习惯。
还有哪个index我也理解错了,应该是现存商品的个数。。
整个代码意思就是一个长度为6的栈,生产者线程不停的生产东西往栈里面存,消费者线程不停的从栈里面取东西。但是这有可能会引起线程安全问题,所以用同步函数来控制
while是为了防止空指针异常的,没地方了放生产的物品了就不能再生产了,同样,栈里面没商品了消费者也就不能取了。
WoTou就是那个商品。

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马