场景1
两个人同时取一个银行账户里面的钱,一个使用银行卡,另一个使用存折,其中一个取钱时另一个不可以取,那么应该如何实现呢?
//代码段1
class GetMoney extends Thread{
static int money = 5000;
int getMoney = 0;
String name;
public GetMoney(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
while (true) {
synchronized ("锁") {
if (money > 0) {
Random random = new Random();
int wantMoney = random.nextInt(100) + 1;
if (money - wantMoney <= 0) {
wantMoney = money;
}
money = money - wantMoney;
getMoney = getMoney + wantMoney;
System.out.println(this.name+"取出了"+wantMoney+"钱,累计"+getMoney+"钱");
try {
sleep(300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
代码段1实现了这种单独访问的方式,但是有随机性,不能实现交替取款的方式,那么还有什么更好的方式嘛?请看代码段2
class Person {
public static int money = 5000;
public static String getMoney_way;
public int bank_getMoney_count;
public int card_getMoney_count;
public int alipay_getMoney_count;
public static int now_getMoney;
public static int type;
boolean flag = false;
}
class Choose_GetMoney_Way implements Runnable{
Person person;
public Choose_GetMoney_Way() {
// TODO Auto-generated constructor stub
}
public Choose_GetMoney_Way(Person p) {
// TODO Auto-generated constructor stub
this.person = p;
}
public void run() {
int i = 0;
while(true){
synchronized (this.person) {
if (this.person.flag == true) {
try {
this.person.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
if (this.person.money > 0) {
Random random = new Random();
int wantMoney = random.nextInt(100) + 1;
if (this.person.money - wantMoney <= 0) {
wantMoney = this.person.money;
}
this.person.money = this.person.money - wantMoney;
if (i == 0) {
this.person.bank_getMoney_count += wantMoney;
this.person.getMoney_way = "存折";
this.person.now_getMoney = wantMoney;
}else if (i == 1) {
this.person.getMoney_way = "银行卡";
this.person.now_getMoney = wantMoney;
this.person.card_getMoney_count += wantMoney;
}else if (i == 2) {
this.person.getMoney_way = "支付宝";
this.person.now_getMoney = wantMoney;
this.person.alipay_getMoney_count += wantMoney;
}
this.person.type = i;
this.person.flag = true;
this.person.notify();
}
i = (i+1)%3;
}
}
}
}
class OutPut_GetMoney_Way implements Runnable{
Person person;
public OutPut_GetMoney_Way() {
// TODO Auto-generated constructor stub
}
public OutPut_GetMoney_Way(Person p) {
// TODO Auto-generated constructor stub
this.person = p;
}
@Override
public void run() {
while(true){
synchronized (this.person) {
if (this.person.flag == false) {
try {
this.person.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
if (this.person.type == 0) {
System.out.println("从"+this.person.getMoney_way+"获取"+this.person.now_getMoney+"元"+" 存折累计获得"+this.person.bank_getMoney_count );
}else if (this.person.type == 1) {
System.out.println("从"+this.person.getMoney_way+"获取"+this.person.now_getMoney+"元"+" 银行卡累计获得"+this.person.card_getMoney_count);
}else if (this.person.type == 2){
System.out.println("从"+this.person.getMoney_way+"获取"+this.person.now_getMoney+"元"+" 支付宝累计获得"+this.person.alipay_getMoney_count);
}
this.person.flag = false;
this.person.notify();
}
}
}
}
调用方式如下:
Person p = new Person();
// GetMoney_Way getMoney_way1 = new GetMoney_Way("存折", p);
// getMoney_way1.start();
//
// GetMoney_Way getMoney_way2 = new GetMoney_Way("银行卡", p);
// getMoney_way2.start();
Choose_GetMoney_Way aChoose_GetMoney_Way = new Choose_GetMoney_Way(p);
OutPut_GetMoney_Way aOutPut_GetMoney_Way = new OutPut_GetMoney_Way(p);
Thread aThread = new Thread(aChoose_GetMoney_Way);
Thread bThread = new Thread(aOutPut_GetMoney_Way);
aThread.start();
bThread.start();