我做了一个模拟游戏中多个人杀一个怪的程序,实现了同步,但是最后一步当怪的血被其中一个人降为0之后,其他2个人(线程)又执行了一次attack(),这肯定是不合理的,请问该怎么解决!!!!!小弟开始JAVA时间不长哈!!
下一贴是代码↓

解决方案 »

  1.   

    public class KillDragon{
    public static void main(String []args){
    System.out.println("   勇者斗恶龙");
    Dragon evil = new Dragon(2000);
    new Hero("Kevin",98,evil).start();
    new Hero("Alex",120,evil).start();
    new Hero("Jacky",200,evil).start();
    }
    }
    class Dragon{
    private int hp;
    Dragon(int x){
    hp=x;
    System.out.println("恶龙的生命值是:"+hp);
    }
    public int gethp(){return hp;}
    public void hpDown(int hurt){
    if(hurt>=hp)hp=0;
    else
    hp-=hurt;
    }
    }
    class Hero extends Thread{
    private String name;
    private int power;
    Dragon evil;
    Hero(String str,int n,Dragon d){
    name=str;
    power=n;
    evil=d;
    System.out.println("英雄 "+name+" 的攻击力是:"+power);
    }
    static synchronized void attack(Hero h){
    int hurt = h.power - (int)(Math.random()*20);
    h.evil.hpDown(hurt);
    System.out.println("英雄 "+h.name+" 对恶龙造成伤害:"+hurt);
    System.out.println("恶龙的生命值还剩下:"+h.evil.gethp());
    try{sleep(1000);}catch(Exception e){}
    }
    public void run(){while(evil.gethp()>0)attack(this);}
    }
      

  2.   

    你同步只同步了attack方法,却没有把while里的判断是否>0包含在同步里,这有什么用呢。
      

  3.   

    我的意思并不是说,只是把while移动到同步里面,而是都的改,判断一定是要在同步里面的,在每个循环里面再调用一下wait不就可以吗,象现在这种写法,有可能其它两个线程还没有启动起来,怪物的血已经完了。
      

  4.   

    能帮我改一下run()和attack()吗,我实在改得不成样子。。
      

  5.   

    你对attack进行synchronized的根本没同步
    简点修改如下:
    public class KillDragon{
    public static void main(String []args){
    System.out.println("   勇者斗恶龙");
    Dragon evil = new Dragon(2000);
    new Hero("Kevin",98,evil).start();
    new Hero("Alex",120,evil).start();
    new Hero("Jacky",200,evil).start();
    }
    }
    class Dragon{
    private int hp;
    Dragon(int x){
    hp=x;
    System.out.println("恶龙的生命值是:"+hp);
    }
    public int gethp(){return hp;}
    public void hpDown(int hurt){
    if(hurt>=hp)hp=0;
    else
    hp-=hurt;
    }
    }
    class Hero extends Thread{
             static String tt="aa";
    private String name;
    private int power;
    Dragon evil;
    Hero(String str,int n,Dragon d){
    name=str;
    power=n;
    evil=d;
    System.out.println("英雄 "+name+" 的攻击力                  是:"+power);
    }
    public  void attack(Hero h){
                     synchronized(tt){while(evil.gethp()>0){
    int hurt = h.power - (int)(Math.random()*20);
    h.evil.hpDown(hurt);
    System.out.println("英雄 "+h.name+" 对恶龙造成伤                  害:"+hurt);
    System.out.println("恶龙的生命值还剩下:"                   +h.evil.gethp());
    try{Thread.sleep(8);}catch(Exception e){}
                    }
    }}
    public void run(){attack(this);}
    }
    楼主给分啊,呵呵!!
      

  6.   

    你直接在同步方法里面加上一个检测HP的句子就行了
    用static 的龙 
    if(evil.gethp>0) 就可以了
      

  7.   

    楼主的根本没有同步因为static synchronized void attack(Hero h)是对this的同步但你的this是不一样的,修改如下:public class KillDragon{
    public static void main(String []args){
    System.out.println("   勇者斗恶龙");
    Dragon evil = new Dragon(2000);
    new Hero("Kevin",98,evil).start();
    new Hero("Alex",120,evil).start();
    new Hero("Jacky",200,evil).start();
    }
    }
    class Dragon{
    private int hp;
    Dragon(int x){
    hp=x;
    System.out.println("恶龙的生命值是:"+hp);
    }
    public int gethp(){return hp;}
    public void hpDown(int hurt){
    if(hurt>=hp)hp=0;
    else
    hp-=hurt;
    }
    }
    class Hero extends Thread{
            static String tt="aa";
    private String name;
    private int power;
    Dragon evil;
    Hero(String str,int n,Dragon d){
    name=str;
    power=n;
    evil=d;
    System.out.println("英雄 "+name+" 的攻击力是:"+power);
    }
    public void attack(Hero h){                { 
                       while(evil.gethp()>0){
                            synchronized(tt){
                            if(evil.gethp()>0)
                              {
                            int hurt = h.power - (int)(Math.random()*20);
           h.evil.hpDown(hurt);
           System.out.println("英雄 "+h.name+" 对恶龙造成伤          害:"+hurt);
           System.out.println("恶龙的生命值还剩下:" +h.evil.gethp());
                           try{Thread.sleep(1000);}catch(Exception e){}                          } }}}} public void run(){
                           attack(this);
                           }}
    楼主给分啊!!
      

  8.   

    while应该放在同步外面,否则线程会独占资源一直while下去。至于楼主所说的问题,可以在attack开头加入一个hp检测,<=0就exit。另外我觉得你的sleep(1000);是不是应该放在attack外面?放在里面sleep也不会释放同步资源,其他线程还是再等待。
      

  9.   

    晕了,在attack里加了个HP的判断真还搞定了,没搞懂为什么while已经判断了怎么里面还要再判断
    ...
    谢谢各位帮忙,打算再做个2个人对砍的,嘿嘿