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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘潘敏 中级黑马   /  2015-4-10 22:27  /  743 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

是我不认真吗?为什么跟毕老师上的视频上讲的一模一样,为什么我的运行不出来结果?
生产者和消费者问题,生产一个,消费一个

package Thread;
class Resource {
        private String name;
        private int count=1;
        private boolean flag=false;
        public synchronized void set(String name){
                if(flag){
                        try{
                                this.wait();
                               
                        }catch(InterruptedException e){
                                e.printStackTrace();
                               
                        }
                        this.name = name + count;
                        count++;
//                        打印生产了那件商品
                        System.out.println(Thread.currentThread().getName()+"......生产者...."+this.name);

                        flag=true;
                        this.notify();//唤醒消费者
                }
               
        }
        public synchronized void out(){
                if(!flag){
                        try{
                                this.wait();
                        }catch(InterruptedException e){
                                e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+" "+count);
                        flag=false;
                        //唤醒生产者
                        this.notify();
                       
                }
        }
}
//生产者类
class Producer implements Runnable{
       
        private Resource r;
        public Producer(Resource r){
                this.r=r;
               
                }
        public void run(){
                while(true){
                        r.set("馒头");
                }
                       
                }
}
// 消费者类
class Consumer implements Runnable {
        private Resource r;
        public Consumer(Resource r){
                this.r=r;
        }
        public void run(){
                while(true){
                        r.out();
                }
        }
       
}
public class ThreadTest3 {
        public static void main(String[] args) {

  Resource r=new Resource();
  Producer p=new Producer(r);
  Consumer c=new Consumer(r);
  Thread t1=new Thread(p);
  Thread t2=new Thread(c);
  t1.start();
  t2.start();
               
               
        }

}

3 个回复

倒序浏览
package Thread; class Resource {         private String name;         private int count=1;         private boolean flag=false;         public synchronized void set(String name){                 if(flag){                         try{                                 this.wait();                                                          }catch(InterruptedException e){                                 e.printStackTrace();                                                          }               //括号是在这里                 }                         this.name = name + count;                         count++; //                        打印生产了那件商品                         System.out.println(Thread.currentThread().getName()+"......生产者...."+this.name);                          flag=true;                         this.notify();//唤醒消费者                 }                                   public synchronized void out(){                 if(!flag){                         try{                                 this.wait();                         }catch(InterruptedException e){                                 e.printStackTrace();                         }           //括号是在这里                 }                         System.out.println(Thread.currentThread().getName()+" "+count);                         flag=false;                         //唤醒生产者                         this.notify();                                          }         }  //生产者类 class Producer implements Runnable{                  private Resource r;         public Producer(Resource r){                 this.r=r;                                  }         public void run(){                 while(true){                         r.set("馒头");                 }                                          } } // 消费者类 class Consumer implements Runnable {         private Resource r;         public Consumer(Resource r){                 this.r=r;         }         public void run(){                 while(true){                         r.out();                 }         }          } public class ThreadTest3 {         public static void main(String[] args) { System.out.println("xxxxxxxxxxx");   Resource r=new Resource();   Producer p=new Producer(r);   Consumer c=new Consumer(r);   Thread t1=new Thread(p);   Thread t2=new Thread(c);   t1.start();   t2.start();                                           }  }
回复 使用道具 举报
package Thread;
class Resource {
        private String name;
        private int count=1;
        private boolean flag=false;
        public synchronized void set(String name){
                if(flag){
                        try{
                                this.wait();
                                
                        }catch(InterruptedException e){
                                e.printStackTrace();
                                
                        }
              //括号是在这里
                }

                        this.name = name + count;
                        count++;
//                        打印生产了那件商品
                        System.out.println(Thread.currentThread().getName()+"......生产者...."+this.name);

                        flag=true;
                        this.notify();//唤醒消费者
                }
               
        
        public synchronized void out(){
                if(!flag){
                        try{
                                this.wait();
                        }catch(InterruptedException e){
                                e.printStackTrace();
                        }
          //括号是在这里
                }

                        System.out.println(Thread.currentThread().getName()+" "+count);
                        flag=false;
                        //唤醒生产者
                        this.notify();
                        
                }
        }

//生产者类
class Producer implements Runnable{
        
        private Resource r;
        public Producer(Resource r){
                this.r=r;
               
                }
        public void run(){
                while(true){
                        r.set("馒头");
                }
                        
                }
}
// 消费者类
class Consumer implements Runnable {
        private Resource r;
        public Consumer(Resource r){
                this.r=r;
        }
        public void run(){
                while(true){
                        r.out();
                }
        }
        
}
public class ThreadTest3 {
        public static void main(String[] args) {
System.out.println("xxxxxxxxxxx");
  Resource r=new Resource();
  Producer p=new Producer(r);
  Consumer c=new Consumer(r);
  Thread t1=new Thread(p);
  Thread t2=new Thread(c);
  t1.start();
  t2.start();
               
               
        }

}
回复 使用道具 举报
黄宝宝 发表于 2015-4-10 23:39
package Thread;
class Resource {
        private String name;

谢谢啦,终于解决啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马