taoge2121 2010-12-29 08:59
浏览 129
已采纳

java基础线程问题求解!

为什么线程Consume 必须用构造啊?我把它改成new的形式就好使,谁能给我解释一下,谢谢!
public class PC {
    public static void main(String[] args) {
        Factory f = new Factory();
        Producer p = new Producer(f);
        Consume c = new Consume();
        new Thread(p).start();
        new Thread(c).start();
        
    }

}

//商品,货物
class Goods {
    int id;

    public Goods(int id) {
        this.id = id;
    }

    // 重写toString方法
    public String toString() {
        return "Goods : " + id;
    }
}

class Factory {
    // 记录物品个数
    int index = 0;
    Goods[] goods = new Goods[10];

    // 制造方法,每次调用,增加一个商品
    public synchronized void produce(Goods g) {
        //当容器已经满了,等待别人消费,wait,调用notify();等待别人消费后叫我再生产
        while(index == goods.length){
            try {
                this.wait();//Object类的wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        goods[index] = g;
        index++;
    }

    // 消费方法
    public Goods consume() {
        synchronized (this) {
            //当容器没有商品了,等待生产者生产,自己wait,调用notify();等待别人生产后叫我去消费
            while(index == 0){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            index--;
            return goods[index];
        }
    }
}

class Producer implements Runnable {
    Factory f = null;

    public Producer(Factory f) {
        this.f = f;
    }

    public void run() {
        for (int i = 0; i < 50; i++) {
            Goods g = new Goods(i);
            f.produce(g);
            System.out.println("生产了:" + g);
            try {//生产一个睡一会儿
                Thread.sleep((int) (Math.random() * 200));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

class Consume implements Runnable {
    Factory f = null;
//  public Consume(Factory f){
//      this.f = f;
//  }
    public void run() {
        for (int i = 0; i < 50; i++) {
            Goods g = new Goods(i);
             f = new Factory();
            f.consume();
            System.out.println("我消费了:" + g);
            try {
                Thread.sleep((int) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 写回答

5条回答 默认 最新

  • beneo 2010-12-29 10:14
    关注

    [quote]
    为什么线程Consume 必须用构造啊?
    [/quote]

    其实你问的问题与多线程无关。你的good实例是facotry#produce方法产生的,good的消费是facotry#consume方法消费的。你的线程无论是Producer还是Consume,都是通过调用facotry实例的相应方法来运行。所以你必须要传入facotry实例,要不然,他怎么知道是哪个facotry实例生产了good,你要消费哪个facotry实例的good

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案