java模拟多用户取款(多线程同步)

package com.multiThreadstudy;

public class Bank {
	protected final static int MUN_ACCOUNTS=8;
	private int[] accounts=new int[MUN_ACCOUNTS];
	public Bank()
	{
		for(int i=0;i<MUN_ACCOUNTS;i++)
		{
			accounts[i]=10000;
		}
	}
	public void showAccounts()
	{
		int total=0;
		for(int i=0;i<Bank.MUN_ACCOUNTS;i++)
		{
			total+=accounts[i];//System.out.println(i);
		}
		System.out.println("银行总存款为"+total);
	}
	public synchronized void transfer(int from,int into,int amount)
	{
		if(accounts[from]>=amount&&from!=into)
		{
			int newAccountFrom=accounts[from]-amount;
			int newAccountInto=accounts[into]+amount;
			try {
				Thread.sleep(3000);
				accounts[from]=newAccountFrom;
				accounts[into]=newAccountInto;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public int getAmount(int id)
	{
		return accounts[id];
	}
}

package com.multiThreadstudy;

public class Customer extends Thread {
	private int customerID;
	private boolean running;
	private Bank bank;
	
	public Customer(int id,Bank bank)
	{
		this.customerID=id;
		this.running=false;
		this.bank=bank;
	}
	
	public void run()
	{
		int into=0;
		int amount=0;
		while(running)
		{
			into=(int)(Bank.MUN_ACCOUNTS*Math.random());
			amount=(int)(bank.getAmount(this.customerID)*Math.random());
			bank.transfer(this.customerID,into,amount);
			yield();			//让其他线程有运行的时间
		}
	}
	
	public void start()
	{
		running=true;
		super.start();
	}
	
	public void halt()
	{
		running=false;
	}
}

package com.multiThreadstudy;

public class test {
	public static void main(String[] args)
	{
		Bank bank=new Bank();
		Customer[] customers=new Customer[Bank.MUN_ACCOUNTS];
		for(int i=0;i<Bank.MUN_ACCOUNTS;i++)
		{
			customers[i]=new Customer(i,bank);
			customers[i].start();
		}
		for(int i=0;i<40;i++)
		{
			bank.showAccounts();
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
如果没有在transfer函数加上 synchronized,哪门银行的总存款会不断的减少。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。以下是 Java 多线程模拟银行取款的代码实现。 ```java public class BankAccount { private int balance; // 银行账户余额 public BankAccount(int initialBalance) { balance = initialBalance; } // 取款方法 public synchronized void withdraw(int amount) { if (balance >= amount) { System.out.println("取款成功,取出 " + amount + " 元"); balance -= amount; } else { System.out.println("余额不足,取款失败"); } } // 查询余额方法 public synchronized void checkBalance() { System.out.println("当前余额为 " + balance + " 元"); } } public class BankClient implements Runnable { private BankAccount account; public BankClient(BankAccount account) { this.account = account; } @Override public void run() { while (true) { // 模拟不定期取款 int amount = (int) (Math.random() * 1000); account.withdraw(amount); try { // 线程休眠,模拟银行服务 Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } account.checkBalance(); } } } public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(10000); BankClient client1 = new BankClient(account); BankClient client2 = new BankClient(account); Thread thread1 = new Thread(client1); Thread thread2 = new Thread(client2); thread1.start(); thread2.start(); } } ``` 以上代码实现了一个简单的 Java 多线程模拟银行取款的程序。程序中,BankAccount 类表示银行账户,包含取款和查询余额的方法。BankClient 类表示银行客户,模拟不定期地取款和查询余额的操作。Main 类是程序的入口,创建了两个 BankClient 实例,并启动了两个线程分别执行客户操作。 注意,在 BankAccount 类的取款方法和查询余额方法上都使用了 synchronized 关键字,确保了线程安全。此外,线程休眠的时间也是随机的,以模拟不同客户不定期的取款和查询操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值