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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 焦健 高级黑马   /  2012-12-25 13:58  /  1003 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 焦健 于 2012-12-27 14:22 编辑

关于我上次提出的多线程生产者消费者问题,实现生产和消费对应起来,0生产0消费,1生产1消费,
今天想想终于做出来了,请大家看看有没有什么不完善的地方
/*多线程执行
*多个生产者消费者同时执行。
*每个上产者生产一个由对应序号的消费者进行消费。
*实现一个生产者对应一个消费者的目的,即00一组,11一组等。
* */
package cn.start;
class Res3 {
        private String name;
        private int count=0;
        private boolean b=false;
        private int x1=0,x2=0;
        public  void set(String name){
               
                this.name=name+count;
                count++;
                System.out.println(Thread.currentThread().getName()+"——生产---"+this.name);
                b=true;
                notifyAll();
        }
        public  void get(){
               
                System.out.println(Thread.currentThread().getName()+"——消费者---"+this.name);
                b=false;
                notifyAll();
        }
        //show1方法的参数是可以运行的线程序号
        public synchronized  void show1(int x){
                //判断线程的名字和位置顺序是否对应,
                if(Thread.currentThread().getName().endsWith(""+x)&&(x==x1)){
                        if(b)
                                try{wait();}catch(Exception e){}
                        set("烤鸭");
                        x1++;
                        if(x1>5)//改变条件值,使它一直循环下去
                                x1=0;
                }
        }
        //show1方法的参数是可以运行的线程序号
        public synchronized void show2(int x){
                //判断线程的名字和位置顺序是否对应,
                if(Thread.currentThread().getName().endsWith(""+x)&& (x==x2)){
                        if(!b)
                                try{wait();}catch(Exception e){}
                        get();
                        x2++;
                        if(x2>5)//改变条件值,使它一直循环下去
                                x2=0;
                }
        }
}
class Shengc implements Runnable{
        Res3 r;

        public Shengc(Res3 r) {
                super();
                this.r = r;
        }
        public void run(){
                while(true){
                        //启动多个show2方法,用for循环实现。
                        for(int y=0;y<=5;y++){
                                r.show1(y);
                                if(y==5)//改变循环条件值,使show2方法能够一直被调用。
                                        y=-1;
                        }
                }
        }
}
class Xiaof implements Runnable{
        Res3 r;
        public Xiaof(Res3 r) {
                super();
                this.r = r;
        }
        public void run(){
                while(true){
                        //启动多个show2方法,用for循环实现。
                        for(int y=0;y<=5;y++){
                                r.show2(y);        
                                if(y==5)//改变循环条件值,使show2方法能够一直被调用。
                                        y=-1;
                        }
                        
                }
        }
}
public class Duo7 {
        public static void main(String[] args) {
               
                Res3 r=new Res3();
                //创建多条线程,线程名字结尾有序号x
                for(int x=0;x<=5;x++){
                new Thread(new Shengc(r),"生产商--"+x).start();
                new Thread(new Xiaof(r),"消费者--"+x).start();
                }
        }

}

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马