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.
我在每次运行该程序时结果可能与上述结果不相同,我想 这是因为多线程中的不同步问题,但是程序哪有问题,各位大大个看看
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.
我在每次运行该程序时结果可能与上述结果不相同,我想 这是因为多线程中的不同步问题,但是程序哪有问题,各位大大个看看
解决方案 »
- 学C++还是Java好?给点意见
- struct2的问题。如何用sendRedirect中止struct2 action的执行
- 一个select里的onchange()事件怎样同时触发多个事件
- Struts里web.xml配置问题 再线急救!!!!
- struts项目中 重启tomcat Dos下显示如下信息????请各位大峡帮忙一下
- 请问:学Struts主要学它的神末?是他的标签吗?
- 哪位大哥可以告诉小弟有关WebLogic的有关内容?
- 请大家帮忙:关于怎么开发EJB Container及其工作原理的问题,可用资源是什么
- 500分求购j2ee本身的源代码
- 给我个花钱用(WebLogic,WebSpere)而不用免费J2EE(Sun)的理由
- MyEclipse中如何使用Hibernate配置多对多映射
- MyEclipse整合spring框架支持时applicationContext.xml上总是有红叉
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()
}
存取的方法钱都加上同步