程序如下:
class SalesLady{
int  memontoes,five,ten;
public synchronized String ruleForSale(int num,int money){
String s=null;
if(memontoes==0) {return "对不起,已售完。\n";}
if(money==5&&memontoes>0){
memontoes--;
five++;
s="给你一个纪念品,你的钱正好。\n";
}else if(money==10&&memontoes>0){
while(five<1){
try{
System.out.println(" "+num+"号顾客用10元购票,发生等待。\n");
wait();
}
  catch(InterruptedException e){}
}
memontoes--;
five--;
ten++;
s="给你一个纪念品,你给了10元找你五元。\n";
}

notify();
return s;
}
SalesLady(int m,int f,int t){
memontoes=m;
five=f;
ten=t;
}
}


public class Example8_5{
static SalesLady salesLady;
static Thread[] aThreadArray;
public static void main(String []args){
    salesLady= new SalesLady(14,0,0); int moneies[]={10,10,5,10,5,10,5,5,10,5,10,5,5,10,5,};
      aThreadArray=new Thread[15];
System.out.println("现在开始购票"+"\n");
for(int i=0;i<moneies.length;i++){
aThreadArray[i]=new Thread(new CustomerClass(i+1,moneies[i]));
       aThreadArray[i].start();
}
     WhileLoop:
     while(true){
      for(int i=0;i<moneies.length;i++)
      if(aThreadArray[i].isAlive())
      continue WhileLoop;
      break;
      }
      System.out.println("购票结束。赚了"+(salesLady.five*5+salesLady.ten*10));
    }
}


class CustomerClass implements Runnable{
int num,money;
public void run(){
try{Thread.sleep(10);
}catch(InterruptedException e){}
System.out.println("我是"+num+"号顾客,用了"+money+"元来够纪念物,售货员说:"+Example8_5.salesLady.ruleForSale(num,money));
}
CustomerClass(int n,int m){num=n;money=m;}

}
这是一个模拟顾客购买纪念品的程序,分别为5元和10元一个纪念品,顾客数组我设定的是15,纪念品的剩余数目memontoes是14,编译没有出错,但是答案没有得到预期效果,应该在最后会输出“已售完的”,但是实际输出的时候好像不一定出现,而且总数经常为75,也出错,不知道错误在哪,新手求救。

解决方案 »

  1.   

    把代码贴在[code = java] 和[/code]之间
      

  2.   


    else if(money==10&&memontoes>0)
    {
    while(five<1)
    {
    try
    {
    System.out.println(" "+num+"号顾客用10元购票,发生等待。\n");
    wait();
    }
    catch(InterruptedException e){}
    }
    memontoes--;
    five--;
    ten++;
    s="给你一个纪念品,你给了10元找你五元。\n";
    }问题应该是出在这里,进入阻塞前,的确memontoes>0的,但是阻塞后恢复,memontoes有可能会小于0的,但是也照常执行了
    class SalesLady
    {
    int memontoes,five,ten;

    SalesLady(int m,int f,int t)
    {
    memontoes=m;
    five=f;
    ten=t;
    }

    public synchronized String ruleForSale(int num,int money)
    {
    String s=null;

    if(memontoes==0) 
    {
    return "对不起,已售完。\n";
    }
    if(money==5&&memontoes>0)
    {
    memontoes--;
    five++;
    s="给你一个纪念品,你的钱正好。\n";
    }
    else if(money==10&&memontoes>0)
    {
    while(five<1)
    {
    try
    {
    System.out.println(" "+num+"号顾客用10元购票,发生等待。\n");
    wait();
    }
    catch(InterruptedException e){}
    }

    //这里再加一个判断语句
    if (memontoes == 0)
    {
    return "对不起,已售完。\n";
    }

    memontoes--;
    five--;
    ten++;
    s="给你一个纪念品,你给了10元找你五元。\n";
    } notify();
    return s;
    }
    }public class Example8_5
    {
    static SalesLady salesLady;
    static Thread[] aThreadArray;

    public static void main(String []args)
    {
    salesLady= new SalesLady(14,0,0);
    int moneies[]={10,10,5,10,5,10,5,5,10,5,10,5,5,10,5,};
    aThreadArray=new Thread[15];
    System.out.println("现在开始购票"+"\n");
    for(int i=0;i<moneies.length;i++)
    {
    aThreadArray[i]=new Thread(new CustomerClass(i+1,moneies[i]));
    aThreadArray[i].start();
    }
    WhileLoop:
    while(true)
    {
    for(int i=0;i<moneies.length;i++)
    {
    if(aThreadArray[i].isAlive())
    {
    continue WhileLoop;
    }
    }
    break;
    }
    System.out.println("购票结束。赚了"+(salesLady.five*5+salesLady.ten*10));
    }
    }class CustomerClass implements Runnable
    {
    int num,money;

    CustomerClass(int n,int m)
    {
    num=n;
    money=m;
    }

    public void run()
    {
    try
    {
    Thread.sleep(10);
    }
    catch(InterruptedException e)
    {}

    System.out.println("我是"+num+"号顾客,用了"+money+"元来够纪念物,售货员说:"+Example8_5.salesLady.ruleForSale(num,money));
    }
    }
      

  3.   

    调试好像还是错误,我尝试了下,在第一个if上加else{}直接括住下面的判断代码,还是得不到效果,郁闷呢。