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

 

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
    评论
好的,您的问题是关于Java多线程存钱取钱问题。这是一个经典的多线程同步问题,可以通过使用synchronized关键字来实现线程安全。下面是一个简单的示例代码: ```java public class BankAccount { private int balance; public BankAccount(int balance) { this.balance = balance; } public synchronized void deposit(int amount) { balance += amount; System.out.println("Deposit successful. Balance: " + balance); } public synchronized void withdraw(int amount) { if (balance < amount) { System.out.println("Withdraw failed. Insufficient balance."); return; } balance -= amount; System.out.println("Withdraw successful. Balance: " + balance); } } public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000); // Create two threads to simulate deposit and withdraw Thread depositThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.deposit(100); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread withdrawThread = new Thread(() -> { for (int i = 0; i < 5; i++) { account.withdraw(200); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); depositThread.start(); withdrawThread.start(); } } ``` 在这个示例代码中,我们创建了一个银行账户类BankAccount,并在其中实现了deposit和withdraw方法,并使用synchronized关键字来保证线程安全。 在main方法中,我们创建了两个线程来模拟存款和取款操作,每个线程执行5次操作。我们使用Thread.sleep方法来模拟每个操作之间的间隔,以便更好地观察多线程操作的结果。 当多个线程同时访问BankAccount对象的deposit和withdraw方法时,synchronized关键字可以确保每个方法只能被一个线程访问,从而避免了竞争条件和数据不一致的问题。 希望这个示例代码能够回答您的问题

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值