java-多线程

场景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();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值