public class L09_04_DepositThread implements Runnable {
Account acc;//用Account类声明一个对象 public L09_04_DepositThread(Account acc) {
this.acc = acc;
} public void run() {
acc.deposit(20.0f);
acc.withdraw(10.0f);
} // run() private static int NUM_OF_THREAD = 100; static Thread[] threads = new Thread[NUM_OF_THREAD]; // 创建线程数组 public static void main(String[] args) {
final Account acc = new Account("王红", 1000.0f);
for (int i = 0; i < NUM_OF_THREAD; i++) {
L09_04_DepositThread my = new L09_04_DepositThread(acc);
threads[i] = new Thread(my); // 创建新线程
threads[i].start(); // 运行线程
}
for (int i = 0; i < NUM_OF_THREAD; i++) {
try {
threads[i].join(); // 等待所有线程运行结束
} catch (InterruptedException e) {
}
}
System.out.println("完成,王红的账户余额为:" + acc.getBalance());
} // main()
}class Account {
String name; float amount; public Account(String name, float amount) {
this.name = name;
this.amount = amount;
} public void deposit(float amt) {
float tmp = amount;
tmp += amt;
try {
Thread.sleep(1);// 模拟其他处理所需要的时间,如刷新数据库等
} catch (InterruptedException e) {
}
amount = tmp;
} // deposit() public void withdraw(float amt) {
float tmp = amount;
tmp -= amt;
try {
Thread.sleep(1);// 模拟其他处理所需要的时间,如刷新数据库等
} catch (InterruptedException e) {
}
amount = tmp;
} // withdraw() public float getBalance() {
return amount;
} // getBalance()
}以上的程序,
模拟银行中的多线程同时对同一个储蓄账户进行存款、取款操作。在主程序中我们首先生成了10个线程,然后启动它们,每一个线程都对同一账户进行存20元,然后又马上取出10元。这样,对于该账户来说,最终账户的余额应该是对原账户存款增加1000元才对,本应输出2000.
我在每次运行该程序时结果可能与上述结果不相同,我想 这是因为多线程中的不同步问题,但是程序哪有问题,各位大大个看看

解决方案 »

  1.   

    我是这样想的:一次存20,取10元相当于一次存10元,100次应该是存入1000元么,是什么情况使得amount的值无法正确修改呢?
      

  2.   


    public class DepositThread implements Runnable {
    Account acc;// 用Account类声明一个对象 public DepositThread(Account acc) {
    this.acc = acc;
    } public void run() {
    acc.deposit(20.0f);
    acc.withdraw(10.0f);
    } // run() private static int NUM_OF_THREAD = 100; static Thread[] threads = new Thread[NUM_OF_THREAD]; // 创建线程数组 public static void main(String[] args) {
    final Account acc = new Account("王红", 1000.0f);
    System.out.println("王红的账户余额为:" + acc.getBalance());
    for (int i = 0; i < NUM_OF_THREAD; i++) {
    DepositThread my = new DepositThread(acc);
    threads[i] = new Thread(my); // 创建新线程
    threads[i].start(); // 运行线程
    }
    for (int i = 0; i < NUM_OF_THREAD; i++) {
    try {
    threads[i].join(); // 等待所有线程运行结束
    } catch (InterruptedException e) {
    }
    }
    System.out.println("完成,王红的账户余额为:" + acc.getBalance());
    } // main()
    }class Account {
    String name; float amount; public Account(String name, float amount) {
    this.name = name;
    this.amount = amount;
    } public synchronized void deposit(float amt) {
    float tmp = amount;
    tmp += amt;
    try {
    Thread.sleep(10);// 模拟其他处理所需要的时间,如刷新数据库等
    } catch (InterruptedException e) {
    }
    amount = tmp;
    } // deposit() public synchronized void withdraw(float amt) {
    float tmp = amount;
    tmp -= amt;
    try {
    Thread.sleep(10);// 模拟其他处理所需要的时间,如刷新数据库等
    } catch (InterruptedException e) {
    }
    amount = tmp;
    } // withdraw() public float getBalance() {
    return amount;
    } // getBalance()
    }
    存取的方法钱都加上同步