生产者和消费者的问题 请教了

下面的程序有点小小问题,先放出问题 求解,下面附上代码

[quote]
期望运行结果:
....//省略了
46
生产者gen03馒头 编号:3
47
生产者gen02馒头 编号:4
48
生产者gen01馒头 编号:5
49
--消费者cus01馒头 编号:5
50
生产者gen03馒头 编号:5
51
生产者gen01馒头 编号:6
生产者gen02馒头 编号:7
53
53
生产者gen03馒头 编号:8
54
生产者gen01馒头 编号:9
55
生产者gen02馒头 编号:10
56


问题点:(注掉了//--------------包围的代码产生的)
....//省略了

48
--消费者cus01馒头 编号:12
49
--消费者cus02馒头 编号:11
50
--消费者cus01馒头 编号:10
51
生产者gen03馒头 编号:10
52
生产者gen02馒头 编号:11
53
生产者gen01馒头 编号:12
54
生产者gen02馒头 编号:13
55
生产者gen01馒头 编号:14
56
生产者gen03馒头 编号:15
57
生产者gen02馒头 编号:16
58
生产者gen03馒头 编号:17
59
生产者gen01馒头 编号:18
60
生产者gen03馒头 编号:19
61
生产者gen02馒头 编号:20
生产者gen01馒头 编号:21
63
63
[/quote]

上代码
[code="java"]
package com.ibm.TestSynchronized;

import java.util.ArrayList;

public class Generant_Customer {
public static void main(String[] args) {
Collection c = new Collection();
Generant gen = new Generant(c);
Customer cus = new Customer(c);

    new Thread(gen,"gen01").start();
    new Thread(gen,"gen02").start();
    new Thread(gen,"gen03").start();
    new Thread(cus,"cus01").start();
    new Thread(cus,"cus02").start();
}

}
/**

  • 资源类 , 消费者和生产者共同操作的资源
    */
    class Collection {
    ArrayList collection = new ArrayList();
    static final int length = 10;
    int index = 1 ;
    int operateCount = 0;

    /* 生产者生产 */
    public synchronized void add(){
    if(collection.size()>=length){
    try {
    wait();
    } catch (InterruptedException e) {
    System.out.println("add() has been interrupted !");
    System.exit(1);
    }
    }
    notifyAll();//唤醒在此对象监视器上等待的单个线程

    collection.add("馒头 编号:"+index);
    System.out.println("生产者" +Thread.currentThread().getName() + collection.get(index-1));
    index ++ ;
    operateCount++;
    }

    /* 消费者消费 */
    public synchronized void pop(){
    if(collection.size() == 0){
    try {
    wait();
    } catch (InterruptedException e) {
    System.out.println("pop() has been interrupted !");
    System.exit(1);
    }
    }
    notifyAll(); //唤醒在此对象监视器上等待的单个线程

    System.out.println("--消费者" +Thread.currentThread().getName() + collection.get(collection.size()-1));
    collection.remove(collection.size()-1);
    index -- ;
    operateCount++;
    }
    }

class Generant implements Runnable{
Collection coll = null ;

public Generant(Collection collection ){
    this.coll = collection ;
}

/* 生产者开始执行 */
public void run() {
    while(true){
        coll.add();
        System.out.println(coll.operateCount);
        //------------------------------------------------------------------
        //注掉后运行就有问题
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            System.out.println("Generant.run() has been interrupted !\\n" +
                    " System continue...");
        }
        //------------------------------------------------------------------
    }
}

}

class Customer implements Runnable{
Collection coll = null ;
/* 线程开关 */
boolean doRun = true ;

public Customer(Collection collection ){
    this.coll = collection ;
}
/* 消费者开始执行 */
public void run() {
    while(doRun){
        coll.pop();
        System.out.println(coll.operateCount);
        //------------------------------------------------------------------
        //注掉后运行就有问题
        try {
            Thread.sleep(150);
        } catch (InterruptedException e) {
            System.out.println("Customer.run() has been interrupted !\\n" +
            " System continue...");
        }
        //------------------------------------------------------------------
        if(coll.operateCount == 50){
            doRun = false;
        }
    }
}

}

[/code]

[quote]在没有被通知、中断或超时的情况下,线程还可以唤醒一个所谓的虚假唤醒 (spurious wakeup)。虽然这种情况在实践中很少发生,但是应用程序必须通过以下方式防止其发生,即对应该导致该线程被提醒的条件进行测试,如果不满足该条件,则继续等待。换句话说,等待应总是发生在循环中,如下面的示例:

synchronized (obj) {
while ()
obj.wait(timeout);
... // Perform action appropriate to condition
}
[/quote]
摘自JDK文档。安全第一!!

Thread.sleep(200);

只是为了模拟你这个操作花了多长时间,你放在add和pop里面 这样更直观

另外我不太理解你有哪些问题?

你也可以看看我的博客 是一篇自己的示例 应该是比你的更清晰点
[url]http://duyunfei.iteye.com/blog/1114189[/url]
不理解的再问

while(true)……
什么问题?
难道是OutOfMemory?

[code="java"]
collection.remove(collection.size()-1);

notifyAll();
[/code]

[code="java"]
collection.add("馒头 编号:"+index);

notifyAll();
[/code]

这样的执行顺序更合理。还有什么问题描述清楚?

基本的问题,无法控制到底是哪个线程被唤醒,所以在
判断竞争条件的地方,一定要用while而不是if。
[code="java"]
if(collection.size()>=length)

if(collection.size() == 0)
[/code]

要修改为
[code="java"]
while(collection.size()>=length)

while(collection.size() == 0)
[/code]

[code="java"] if(coll.operateCount == 50){

doRun = false;

} [/code]
你是不是指这个有问题?实际上是operateCount 也要同步访问,要不然程序有可能达不到coll.operateCount == 50的条件,改成coll.operateCount >= 50,或者严格控制operateCount 的并发访问

[code="java"]
package bluechip.lang.problem;

import java.util.ArrayList;

public class Generant_Customer {
public static void main(String[] args) {
Collection c = new Collection();
Generant gen = new Generant(c);
Customer cus = new Customer(c);

    new Thread(gen, "gen01").start();
    new Thread(gen, "gen02").start();
    new Thread(gen, "gen03").start();
    new Thread(cus, "cus01").start();
    new Thread(cus, "cus02").start();
}

}

/**

  • 资源类 , 消费者和生产者共同操作的资源
    */
    class Collection {
    ArrayList collection = new ArrayList();
    static final int length = 10;
    int index = 1;
    private int operateCount = 0;// 不可外部直接访问

    /* 生产者生产 */
    public synchronized int add() {
    // 防止虚假唤醒 (spurious wakeup)
    while (collection.size() >= length) {
    try {
    wait();
    } catch (InterruptedException e) {
    System.out.println("add() has been interrupted !");
    System.exit(1);
    }
    }

    collection.add("馒头  编号:" + index);
    System.out.println("生产者" + Thread.currentThread().getName() + collection.get(index - 1));
    index++;
    operateCount++;
    notifyAll();// 唤醒在此对象监视器上等待的单个线程
    return operateCount;
    

    }

    /* 消费者消费 */
    public synchronized int pop() {
    // 防止虚假唤醒 (spurious wakeup)
    while (collection.isEmpty()) {
    try {
    wait();
    } catch (InterruptedException e) {
    System.out.println("pop() has been interrupted !");
    System.exit(1);
    }
    }

    System.out.println("--消费者" + Thread.currentThread().getName()
            + collection.get(collection.size() - 1));
    collection.remove(collection.size() - 1);
    index--;
    operateCount++;
    notifyAll(); // 唤醒在此对象监视器上等待的单个线程
    return operateCount;
    

    }
    }

class Generant implements Runnable {
Collection coll = null;
boolean doRun = true;
public Generant(Collection collection) {
this.coll = collection;
}

/* 生产者开始执行 */
public void run() {
    while (doRun) {
        int c = coll.add();
        System.out.println(c);
        // ------------------------------------------------------------------
        // 注掉后运行就有问题
        // try {
        // Thread.sleep(200);
        // } catch (InterruptedException e) {
        // System.out.println("Generant.run() has been interrupted !\\n"
        // + " System continue...");
        // }
        // ------------------------------------------------------------------
        if (c == 50) {//也有可能在这里达到50
            doRun = false;
        }
    }
}

}

class Customer implements Runnable {
Collection coll = null;
/* 线程开关 */
boolean doRun = true;

public Customer(Collection collection) {
    this.coll = collection;
}

/* 消费者开始执行 */
public void run() {
    while (doRun) {
        int c = coll.pop();
        System.out.println(c);
        // ------------------------------------------------------------------
        // 注掉后运行就有问题
        // try {
        // Thread.sleep(150);
        // } catch (InterruptedException e) {
        // System.out.println("Customer.run() has been interrupted !\\n"
        // + " System continue...");
        // }
        // ------------------------------------------------------------------
        if (c == 50) {
            doRun = false;
        }
    }
}

}

[/code]

说实话,这代码不是很好