线程通信---银行存钱取钱问题

 

Acount.java

Acount类,包括存钱,取钱的方法

package com.Thread;
class Acount{
	private String no;//ID
	private double count;//银行卡余额
	private boolean flag = false;
	
	public Acount(String no,double count){
		this.no = no;
		this.count = count;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public double getCount() {
		return count;
	}

	public void setCount(double count) {
		this.count = count;
	}
	//同步方法,属于对象,所以此处的监视锁相当于this
	public synchronized void draw(double drawAcount){
		if(flag){
			if(drawAcount <= this.getCount()){
				//System.out.println("取钱成功,吐出钞票"+drawAcount);
				//修改余额
				this.setCount(this.getCount()-drawAcount);
				System.out.println("取钱成功,余额为:"+this.getCount());
				
				flag = false;
				//取钱结束后,唤醒wait()的线程
				this.notifyAll();
			}
			else{
				System.out.println("余额不足,取钱失败");
			}
		}else{
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	//同步方法,属于对象,所以此处的监视锁相当于this
	public synchronized void dispos(double disposAcount){
		    //修改余额
		if(!flag){
			this.setCount(this.getCount()+disposAcount);
			
			System.out.println("存钱成功,余额为:"+this.getCount());
			
			flag = true; 
			//存钱结束后,唤醒wait()的线程
			this.notifyAll();
		}	
		else{
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


 

disposThread.java

存钱线程

//存钱线程
package com.Thread;

public class disposeThread extends Thread {
	private Acount acount;
	private double disposAcount;
	public disposeThread(Acount acount,double disposAcount){
		this.acount = acount;
		this.disposAcount = disposAcount;
	}
	public void run() {
		//System.out.println("存钱线程:");
		for(int i = 0; i< 10;++i){
			System.out.println("存钱线程:"+i);
			acount.dispos(disposAcount);
		}
		  
    }

}


 

DrawThread2.java

取钱线程,主函数

package com.Thread;
//使用Acount类
public class DrawThread2 extends Thread {
	private Acount acount;
	private double drawAcount;
	
	public DrawThread2(Acount acount,double drawAcount){
		this.acount = acount;
		this.drawAcount = drawAcount;
	}
	public void run() {
	//	System.out.println("取钱线程:");
		for(int i = 0;i<10;i++){
			System.out.println("取钱线程:"+i);
			acount.draw(drawAcount);  
		}
    }
	public static void main(String[] args) {
		 Acount acount = new Acount("lily",1000.0);
		 //启动两个并发的线程对acount取钱
		 //new DrawThread(acount,1000.0).start(); 
		 //new DrawThread(acount,1000.0).start();	
		
		 new disposeThread(acount,1000.0).start();
		 new DrawThread2(acount,1000.0).start();
    }
}


其中一次的执行结果

存钱线程:0
取钱线程:0
存钱成功,余额为:2000.0
存钱线程:1
取钱成功,余额为:1000.0
取钱线程:1
存钱线程:2
存钱成功,余额为:2000.0
存钱线程:3
取钱线程:2
取钱成功,余额为:1000.0
取钱线程:3
存钱线程:4
存钱成功,余额为:2000.0
存钱线程:5
取钱线程:4
取钱成功,余额为:1000.0
取钱线程:5
存钱线程:6
存钱成功,余额为:2000.0
存钱线程:7
取钱线程:6
取钱成功,余额为:1000.0
取钱线程:7
存钱线程:8
存钱成功,余额为:2000.0
存钱线程:9
取钱线程:8
取钱成功,余额为:1000.0
取钱线程:9


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值