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请大家帮帮我.

解决方案 »

  1.   

    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();
    }
    }
    }
    }
      

  2.   

    注释掉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
      

  3.   

    思路不对
    ATM应该是只有一个吧.你做了两个线程,有了两个ATM了.
      

  4.   

    我做了一个,楼主看看
    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
      

  5.   

    你把你自己的代码再跟我的代码(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楼的,呵呵,或者把你自己的代码改下,统一用一把对象锁就可以了
      

  6.   

    可能楼主还不明白为什么输出就是980,960。好像是用同一个变量,那只是因为你把这个变量设成是静态的
    如果是成员变量,那输出肯定是
    person1  withDraw 
    the rest of money980.0 
    person2  withDraw 
    the rest of money980.0 
    不信你试试