求教:多线程问题,在线等~~~ 我做了一个模拟游戏中多个人杀一个怪的程序,实现了同步,但是最后一步当怪的血被其中一个人降为0之后,其他2个人(线程)又执行了一次attack(),这肯定是不合理的,请问该怎么解决!!!!!小弟开始JAVA时间不长哈!!下一贴是代码↓ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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);}} 你同步只同步了attack方法,却没有把while里的判断是否>0包含在同步里,这有什么用呢。 我的意思并不是说,只是把while移动到同步里面,而是都的改,判断一定是要在同步里面的,在每个循环里面再调用一下wait不就可以吗,象现在这种写法,有可能其它两个线程还没有启动起来,怪物的血已经完了。 能帮我改一下run()和attack()吗,我实在改得不成样子。。 你对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);}}楼主给分啊,呵呵!! 你直接在同步方法里面加上一个检测HP的句子就行了用static 的龙 if(evil.gethp>0) 就可以了 楼主的根本没有同步因为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); }}楼主给分啊!! while应该放在同步外面,否则线程会独占资源一直while下去。至于楼主所说的问题,可以在attack开头加入一个hp检测,<=0就exit。另外我觉得你的sleep(1000);是不是应该放在attack外面?放在里面sleep也不会释放同步资源,其他线程还是再等待。 晕了,在attack里加了个HP的判断真还搞定了,没搞懂为什么while已经判断了怎么里面还要再判断...谢谢各位帮忙,打算再做个2个人对砍的,嘿嘿 哪位大侠知道 window压缩 linux解压缩 java程序解压缩出来的文件带%5C是怎么回事? 新手求助,请各位前辈前来指点,小的在此拜谢!!关于JAVA IO这块知识! 爱立信最新面试题 swing 和 awt?? 请问如何在Applet中链接JSP或者Servlet preparestatement传参数的问题 新年快乐,散分 众位请上眼!不公啊,大大的不公! 搜索引擎的问题 Java基础 插入JTextPane中的图片如何保存起来,可是在rtf或html中保存么? NullPointerException问题。很明显,但是我就是不知道错在什么地方。
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);}
}
简点修改如下:
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);}
}
楼主给分啊,呵呵!!
用static 的龙
if(evil.gethp>0) 就可以了
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);
}}
楼主给分啊!!
...
谢谢各位帮忙,打算再做个2个人对砍的,嘿嘿