java多线程--银行存款取款
来源:互联网 发布:2017淘宝店女装排行榜 编辑:程序博客网 时间:2024/05/01 05:12
tom和kaite共有一个银行账户,初始账目为 1000,某一时刻银行操作员发现tom和kate同时对账户操作,分别取款和存款,各操作10次,每次金额为1000.用java多线程的思想来描述。
对题目进行分析,两人共有一个银行账户,为Account类;两人同时操作,涉及会引起数据不一致,采用同步操作。
package com.accout;
public class Account {
private float saving = 1000;
private float varient = 0;
//每次存款后,将操作人存款额打印出来
public synchronized void add(String name , float varient){
this.varient = varient;
saving += this.varient;
System.out.println(name + " saving " + varient + " to the account ");
System.out.println("the current account has " + saving );
}
public synchronized void withdraw(String name , float varient){
this.varient = varient;
saving -= this.varient;
System.out.println(name + " withdraw " + varient + " from the account ");
System.out.println("the current account has " + saving );
}
}
同时对账户进行操作,创建操作类Operation,操作类有存款和取款方法,分别开启线程,调用Account类的方法;
package com.accout;
public class Operation {
Account account = new Account();
public void addOperation (final String name ,final float varient){
//加载线程
new Runnable(){
@Override
public void run() {
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
//存钱
getAccount().add(name, varient);
}
}.run(); //别忘了加run()
}
public void withdrawOperation (final String name ,final float varient){
//加载线程
new Runnable(){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//取钱
getAccount().withdraw(name, varient);
}
}.run();
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
客户端调用Client类
package com.accout;
public class Client{
public static void main(String[]args){
Operation oper = new Operation();
for(int i=0 ; i< 10 ; i ++){
oper.addOperation("tom", 1000);
oper.withdrawOperation("kaite", 1000);
}
}
}
结果显示:
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
tom saving 1000.0 to the account
the current account has 2000.0
kaite withdraw 1000.0 from the account
the current account has 1000.0
- java多线程--银行存款取款
- 银行存款、取款模型
- Java多线程银行取款
- java多线程-多线程同步代码块-银行存款实例-锁synchronized
- 简单的银行存款取款业务模拟程序
- qt 类和对象 银行存款取款
- java模拟多用户取款(多线程同步)
- Java多线程银行存取款程序
- Java-多线程 银行取款小练习
- java基础******银行存款
- 用JAVA中的多线程示例银行取款问题
- 用JAVA的多线程实现银行取款的问题
- java多线程问题中简单的存取款实现
- 模拟银行存款(进程和多线程)
- java模拟取款机
- java简单取款机
- Java 账户 ,存款、取款
- java线程存取款
- 笔试
- 深入理解sizeof(1)
- [UNIX环境高级编程第二版]读书笔记8章[TBD]
- 地铁网络(分层图-哈密尔顿路径)
- 使用hibernate属性反向生成数据库表
- java多线程--银行存款取款
- VNWare+MAC+J2Objc+Eclipse 环境配置和搭建---(J2Objc的使用)
- HDU 1564 Play a game(规律博弈)
- 深入理解sizeof(2)
- Ubuntu 12.04安装QQ
- nyoj 290 动物统计加强版
- myEclipse配置tomcat
- 单点登录笔记
- 关于A/D的精度、分辨率与LSB误差的扫盲(转载)