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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 左耳的鱼 中级黑马   /  2013-7-18 12:43  /  1286 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 左耳的鱼 于 2013-7-21 17:54 编辑

package cn.itcast.threaddemo;
//定义产品类,有品名,生产个数
class Product {
private String name ;
//统计次数变量
private int count = 1;//这里为什么重1开始????????????
private boolean b = false;

//定义生产方法
public  synchronized void proc(){//是不是先生产,后消费,但是count已经为1了,那还是先生产吗??????
  while(b){//这里是不是b为true就等待,如果不为true,就执行while后面程序。
   try{this.wait();}catch(InterruptedException e){}
  }
  this.name  ="智能手机  生产第 "+ count+"个";
  System.out.println(Thread.currentThread().getName()+this.name);
  count++;//如果这里先累加,后输出生产几个语句的话,是不是count可以设为1?????????????
  b = true;
  this.notifyAll();
}
//定义消费方法
public synchronized void cus(){
  while(!b){
   try{this.wait();}catch(InterruptedException e){}
  }
  System.out.println(Thread.currentThread().getName()+"  "+"消费第   "
   +this.name);
  b = false;
  this.notifyAll();
}
}
//生产者类
class Producter implements Runnable{
private Product p;
    Producter(Product p) {this.p = p;}
    public void run(){
     while(true){
      p.proc();
     }
    }
}
//消费者类
class Customer implements Runnable{
private Product p;
Customer(Product p) {this.p = p;}
public void run(){
  while(true){
  p.cus();
  }
}
}
public class ProductCustomThread {
public static void main(String[] args) {
  Product p = new Product();//产品对象
  Producter pd = new Producter(p);//生产者对象
  Customer ct = new Customer(p);//消费者对象
  
  Thread t1 = new Thread(pd);
  Thread t2 = new Thread(ct);
  
  
  Thread t3 = new Thread(pd);
  Thread t4 = new Thread(ct);
  
  Thread t5 = new Thread(pd);
  Thread t6 = new Thread(ct);
  
  Thread t7 = new Thread(pd);
  Thread t8 = new Thread(ct);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
  t5.start();
  t6.start();
  t7.start();
  t8.start();
  
  
}
}
//哪位大侠能讲下这个逻辑,有点迷糊b的作用

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

4 个回复

倒序浏览
{:soso_e134:}
回复 使用道具 举报
能不能排版一下啊,用代码块的格式啊,太乱了啊
回复 使用道具 举报
    b的作用是:因为你是notifyAll(),所以会唤醒所有的线程,当然包括本方的线程。如果唤醒本方的线程,则不希望它参与,所以b的作用是为了防止本方线程多次参与。
这样才可以达到生产一个消费一个的目的!!!!
回复 使用道具 举报
本帖最后由 牛海亮 于 2013-7-19 18:21 编辑

我把Product类修改了一下,你看下注释看能弄明白不,能理解类中变量b的含义程序就好懂了。

  1. <P>public class Product {
  2.         private String name ;        
  3.         private int count = 0;//刚开始盒子里没有手机,并且盒子里只能放下一个手机,所以需要生产一个消费一个
  4.         private boolean b = false;//盒子是不是满的,true为满,false为空

  5.         //定义生产方法
  6.         public  synchronized void proc(){
  7.           while(!b)
  8.           {           
  9.                                     
  10.                           count++;
  11.                           this.name  ="生产第 "+ count+"个智能手机";
  12.                           System.out.println(Thread.currentThread().getName()+this.name);         
  13.                           b = true;//盒子满了
  14.                           this.notify();//盒子满了,唤醒消费进程进行消费
  15.                           try
  16.                           {   //由于盒子满了,所以不能再生产了,需要等待
  17.                                 this.wait();
  18.                           }
  19.                           catch (Exception e)
  20.                           {
  21.                                 e.printStackTrace();
  22.                           }                          
  23.           }         
  24.         }
  25.         //定义消费方法
  26.         public synchronized void cus(){
  27.           while(b)
  28.           {           
  29.            System.out.println(Thread.currentThread().getName()+"消费第"+count+"个智能手机");           
  30.            b = false;//盒子空了
  31.            this.notify();//唤醒生产者进程        
  32.            try
  33.            {
  34.                    //由于盒子空了,所以无法消费了,需要等待。
  35.                    this.wait();                  
  36.            }
  37.            catch(InterruptedException e)
  38.            {
  39.                    e.printStackTrace();
  40.            }
  41.           }
  42.          
  43.          
  44.         }
  45. }
  46. </P>
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马