xiaotao2010 2011-10-14 11:05
浏览 344
已采纳

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

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

[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]

  • 写回答

7条回答 默认 最新

  • _1_1_7_ 2011-10-18 15:24
    关注

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

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

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!