一个关于多线程的问题?请高手们帮帮我

跑路的蛤蟆 2008-04-16 05:51:26

public class ThreadRunnable{
public static void main(String[] args){
Person.t1.start();
Person.t2.start();

}

}
class Person implements Runnable{
public int i;
static Thread t1 = new Thread(new Person(1));
static Thread t2 = new Thread(new Person(2));
public Person(int i){
this.i = i;
}
ATM atm = new ATM();
public void run() {
synchronized(atm){
if(i==1){

try {
ATM.withDraw(20,i);
atm.wait();
ATM.dispose(20, i);
atm.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}

}else{

try {
ATM.withDraw(20, i);
atm.notify();
atm.wait();
ATM.dispose(20, i);

} catch (InterruptedException e) {

e.printStackTrace();
}

}


}

}

}
class ATM{
static double total=1000;

public static void dispose(double x,int i){
total = total +x;
System.out.println("person"+i+" dispose");
System.out.println(ATM.getTotals());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {

e.printStackTrace();
}
}

public static void withDraw(double x,int i){
total = total -x;
System.out.println("person"+i+" withDraw");
System.out.println("the rest of money"+ATM.getTotals());
try {
Thread.sleep(8000);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
public static double getTotals(){
return total;
}

}

这是程序,我想让它的结果是:
person1 withDraw
the rest money 980
person2 withDraw
the rest money 960
person1 dispose
the rest money 980
person2 dispose
the rest money 1000

但是它现在有问题.不是期望的结果.5555555555请大家帮帮我.
...全文
150 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
whmjxa 2008-04-16
  • 打赏
  • 举报
回复
可能楼主还不明白为什么输出就是980,960。好像是用同一个变量,那只是因为你把这个变量设成是静态的
如果是成员变量,那输出肯定是
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money980.0
不信你试试
whmjxa 2008-04-16
  • 打赏
  • 举报
回复
你把你自己的代码再跟我的代码(8楼)看看就知道问题在哪了
我初步看了下你的代码,发现你这里有个问题
ATM atm = new ATM(); //这里,你每开启一个线程,他都新建一个ATM对象
public void run() {
synchronized(atm){ //这里,同步块中的锁只是这个线程中的atm对象锁
当另一个线程到来时,两个线程所用的锁是不同的,
else{

try {
ATM.withDraw(20, i);
atm.notify(); //你在这里调用notify()方法并不能把第一个线程中同步快中的锁给解锁了
atm.wait();
ATM.dispose(20, i);

所以呢,你这个程序最后输出的结果肯定是
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money960.0
而不是你理想中的输出,想要达到理想中的输出,就用我的代码吧,8楼的,呵呵,或者把你自己的代码改下,统一用一把对象锁就可以了
跑路的蛤蟆 2008-04-16
  • 打赏
  • 举报
回复
我是想问问,我的程序到底有什么问题?请高手们指点一下
whmjxa 2008-04-16
  • 打赏
  • 举报
回复
我做了一个,楼主看看
public class t {
public static void main(String args[]){
B b=new B();
Person person1=new Person(b);
Person person2=new Person(b);
person1.j=200;
person1.k=500;
person2.j=300;
person2.k=600;
new Thread(person1).start();
new Thread(person2).start();
}

}
class Person implements Runnable{
B b;
public Person(B b){
this.b=b;
}
int j=0;
int k=0;
public void run(){
b.get(j);
b.put(k);
}
}
class B{
int i=1000;
boolean b=true;
public synchronized void get(int j){
if(b){
i-=j;
try {
System.out.println(Thread.currentThread().getName()+" comes,gets money"+j+"元");
System.out.println("the rest money is"+i);
notify();
wait();
b=false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void put(int j){
if(!b){
i+=j;
System.out.println(Thread.currentThread().getName()+"puts money"+j+"元");
System.out.println("the rest money is"+i);
b=true;
notify();
}
else{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出结果
Thread-0 comes,gets money200元
the rest money is800
Thread-1 comes,gets money300元
the rest money is500
Thread-0puts money500元
the rest money is1000
Thread-1puts money600元
the rest money is1600
跑路的蛤蟆 2008-04-16
  • 打赏
  • 举报
回复
我在机子上试了,还是不行.
跑路的蛤蟆 2008-04-16
  • 打赏
  • 举报
回复
一楼
跑路的蛤蟆 2008-04-16
  • 打赏
  • 举报
回复
能够具体告诉我为什么那样做吗?为什么我的不正确?
awusoft 2008-04-16
  • 打赏
  • 举报
回复
思路不对
ATM应该是只有一个吧.你做了两个线程,有了两个ATM了.
kevinchj 2008-04-16
  • 打赏
  • 举报
回复
来迟了……
郁闷!
再有问题,问我……
我是风 2008-04-16
  • 打赏
  • 举报
回复
注释掉atm.wait();和atm.notify();
加一行Thread.sleep(0);
结果:
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money960.0
person1 dispose
980.0
person2 dispose
1000.0

我是风 2008-04-16
  • 打赏
  • 举报
回复
class Person implements Runnable {
public int i;

static Thread t1 = new Thread(new Person(1));

static Thread t2 = new Thread(new Person(2));

public Person(int i) {
this.i = i;
}

ATM atm = new ATM();

public void run() {
synchronized (atm) {
try {
if (i == 1) {
ATM.withDraw(20, i);
// atm.wait();
ATM.dispose(20, i);
// atm.notify();
} else {
ATM.withDraw(20, i);
// atm.notify();
// atm.wait();
ATM.dispose(20, i);
}
Thread.sleep(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧