代码如下:
package pack.java.thread.atm;
/**
* 账户类;
* @author Administrator
*
*/
public class Account {
private String name; //用户名;
private int value; //账户余额;
/**
* 存入金额;
* @param monery
*/
public void putMonery(int monery){
this.value = this.value + monery;
}
/**
* 取出金额;
* @param monery
* @return 金额;
*/
public int getMonery(int monery){
//判断是否账户余额是否大于 要取出的钱;
if(this.value monery ){
this.value = this.value - monery;
}else{
monery = this.value; //账户余额不够时,则取出,所有的账户余额的金额.
this.value = 0;
}
//返回取出的钱;
return monery;
}
/**
* 查询余额;
* @return 返回账户余额;
*/
public int search(){
return this.value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
java代码:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)代码如下:
import java.io.Serializable;
/**
* 取款线程类;
* @author Administrator
*
*/
public class FetchMoney extends Thread implements Serializable{
private static final long serialVersionUID = -5059599151558445815L;
private Account account; //账户对象;
private int monery; //余额;
public FetchMoney(){
super();
}
public FetchMoney(int monery,Account account){
this.account = account;
this.monery = monery;
}
@Override
public void run() {
// TODO Auto-generated method stub
int currMonery = account.search(); //当前余额;
synchronized (account) {
try {
sleep(5); //取款发费的时间;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取出余额;
int getMonery = account.getMonery(monery);
System.out.println("尊敬的"+account.getName()+"用户您好!"+"当前的余额为:"+currMonery +"元."+"取出余额为:"+getMonery+"元."+"账户余额为:"+account.search()+"元");
}
}
}
java代码:
代码如下:
/**
* 存款线程类;
* @author Administrator
*
*/
public class SaveMoney extends Thread implements Serializable{
private static final long serialVersionUID = 3095120546560212724L;
private Account account; //账户对象;
private int money; //金额;
public SaveMoney(){
super();
}
public SaveMoney(int money,Account account){
this.account = account;
this.money = money;
}
@Override
public void run() {
// TODO Auto-generated method stub
int currMonery = account.search(); //查询账户余额;
//同步对象,只允许单线程操作;
synchronized (account) {
try {
sleep(5); //查询的时候,发费得时间;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
account.putMonery(this.money); //插入金额;
//输出存款信息;
System.out.println("尊敬的"+account.getName()+"用户您好!"+"当前的余额为:"+currMonery +"元."+"存入余额为:"+this.money+"元."+"账户余额为:"+account.search()+"元");
}
}
}
java代码:
代码如下:
/**
* ATM测试类;
* @author Administrator
*
*/
public class ThreadATMDemo {
/**
* 主方法;
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account();
//设置用户名;
account.setName("ZhouHaiTao");
//初始化余额;
account.setValue(0);
//存入100;
new SaveMoney(100,account).start();
//存入200;
new SaveMoney(200, account).start();
//取出500;
new FetchMoney(500, account).start();
}
}
程序运行结果如下:
尊敬的ZhouHaiTao用户您好!当前的余额为:0元.存入余额为:100元.账户余额为:100元
尊敬的ZhouHaiTao用户您好!当前的余额为:0元.存入余额为:200元.账户余额为:300元
尊敬的ZhouHaiTao用户您好!当前的余额为:0元.取出余额为:300元.账户余额为:0元