模拟网上取钱
题目:小明银行卡有10000元
小明老婆和小明女儿和小明进行存储.
package org.thread.demo.acount;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
/**
* 创建账号信息
* */
Account account = new Account();
account.setAmount(10000);
/**小明信息*/
Scanner sc = new Scanner(System.in);
System.out.println("小明输入账号");
String account0 = sc.next();
System.out.println("小明输入密码");
String password0 = sc.next();
System.out.println("1取钱 2 存钱");
String operation = sc.next();
System.out.println("输入金额");
double unit = sc.nextInt();
account.setAccount(account0);
account.setPassword(password0);
/**小明老婆信息*/
System.out.println("小明老婆输入账号");
String account1 = sc.next();
System.out.println("小明老婆输入密码");
String password1 = sc.next();
System.out.println("1取钱 2 存钱");
String operation1 = sc.next();
System.out.println("输入金额");
double unit1 = sc.nextInt();
account.setAccount(account1);
account.setPassword(password1);
/**小明女儿*/
System.out.println("小明女儿输入账号");
String account2 = sc.next();
System.out.println("小明女儿输入密码");
String password2 = sc.next();
System.out.println("1取钱 2 存钱");
String operation2 = sc.next();
System.out.println("输入金额");
double unit2 = sc.nextInt();
account.setAccount(account2);
account.setPassword(password2);
DrawBinkThread th1 = new DrawBinkThread(account,operation,unit);
DrawBinkThread th2 = new DrawBinkThread(account,operation1,unit1);
DrawBinkThread th3 = new DrawBinkThread(account,operation2,unit2);
th1.setName("小明");
th2.setName("小明老婆");
th3.setName("小明女儿");
/**线程准备*/
th1.start();
th2.start();
th3.start();
}
}
package org.thread.demo.acount;
public class DrawBinkThread extends Thread{
private Account account;
private String opertion;
private double unit;
public DrawBinkThread(Account account, String opertion, double unit) {
super();
this.account = account;
this.opertion = opertion;
this.unit = unit;
}
public void setAccount(Account account) {
this.account = account;
}
@Override
public void run() {
account.suspend(account,opertion,unit);
System.out.println(this.getName()+"取完钱余额为"+account.getAmount());
}
public String getOpertion() {
return opertion;
}
public void setOpertion(String opertion) {
this.opertion = opertion;
}
public double getUnit() {
return unit;
}
public void setUnit(double unit) {
this.unit = unit;
}
public Account getAccount() {
return account;
}
}
package org.thread.demo.acount;
/**
* 银行账号类
*
* */
public class Account {
//账号
private String account;
//密码
private String password;
//余额
private volatile double amount;
/**
* 取存
* */
//加上同步机制使得线程安全
/*方法一 public synchronized void suspend(Account z, String x ,double y) { */
public void suspend(Account z, String x ,double y) {
/**方法2*/
synchronized (z) {
if(y<=z.getAmount() && z.getAccount().equals("1") && z.getPassword().equals("1")) {
if(x.equals("1")) {
//取现
try {
//来一段线程不安全的代码 在睡眠的时候同时取一万的人都进来了,卡里余额变成负数
/**
* 1网略不好的时候
* cpu把资源给别的线程
* */
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
amount=amount-y;
this.setAmount(amount);
}
if(x.equals("2")){
amount=amount+y;
this.setAmount(amount);
}
}else {
System.out.println(Thread.currentThread().getName()+"信息有误");
}
}
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Account [account=" + account + ", password=" + password + ", amount=" + amount + "]";
}
}