JAVA取钱多线程实验_Java多线程中的存款取款问题(涉及到synchronized)

账户文件

Account.java

public class Account {

private String accountNo;

private double balance;

//标识账户中是否已有存款

private boolean flag = false;

public Account(String accountNo,double balance)

{

this.accountNo = accountNo;

this.balance = balance;

}

public double getBalance()

{

return balance;

}

public String getAccount()

{

return accountNo;

}

public synchronized void draw(double drawAmount)

{

try {

//如果里面没钱,则等待(不能取钱)

if (!flag)

{

wait();

}

else {

//取钱

System.out.println(Thread.currentThread().getName()

+ "取钱成功!吐出钞票:"+drawAmount);

balance -= drawAmount;

System.out.println("\t余额为"+balance);

//取钱之后,设置为不能取钱

flag = false;

notifyAll();

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public synchronized void deposit(double depositAmount)

{

try

{

if(flag)

{

//flag为true表示已经有人存钱,则等待(不能存钱)

wait();

}

else {

//可以存钱

System.out.println(Thread.currentThread().getName()

+"存钱成功"+depositAmount);

balance += depositAmount;

flag = true;

System.out.println("账户余额为:"+balance);

notifyAll();

}

}

catch(InterruptedException e)

{

e.printStackTrace();

}

}

public int hashCode()

{

return accountNo.hashCode();

}

//判断是否为同一个对象

public boolean equals(Object obj)

{

if (obj != null && obj.getClass() == Account.class)

{

Account target = (Account) obj;

return target.getAccount().equals(accountNo);

}

return false;

}

}

用于取钱的线程

DrawThread.java

public class DrawThread extends Thread{

private Account account;

private double drawAmount;

public DrawThread(String name,Account account,double drawAmount)

{

super(name);

this.account = account;

this.drawAmount = drawAmount;

}

public void run()

{

//模拟取钱操作

for(int i = 0;i<100;i++)

{

account.draw(drawAmount);

}

}

}

用于存钱的线程

DepositThread.java

public class DepositThread extends Thread{

private Account account;

private double depositAmount;

public DepositThread(String name,Account account,double depositAmount)

{

super(name);

this.account = account;

this.depositAmount = depositAmount;

}

public void run()

{

for (int i = 0;i<100;i++)

{

account.deposit(depositAmount);

}

}

}

主程序

TestDraw.java

public class TestDraw {

public static void main(String[] args) {

Account acct = new Account("1234567", 0);

new DrawThread("取钱", acct, 800).start();

new DepositThread("存钱者甲", acct, 800).start();

new DepositThread("存钱者乙", acct, 800).start();

new DepositThread("存钱者丙", acct, 800).start();

}

}

程序运行时,三个存款者线程随机向一个账户中存钱,只有一个取款者线程执行取钱操作。只有当取款者取钱之后,存款者才能存钱。同样,只有当存款者存款之后,取款者才能取钱。

运行结果:

77b29aebb0ed

运行结果.png

最后卡在了账户余额为800元,程序阻塞。

当使用Lock对象来保证同步时,程序改写为

Account.java

public class Account {

private String accountNo;

private double balance;

//标识账户中是否已有存款

private boolean flag = false;

private Lock lock = new ReentrantLock();

private Condition cond = lock.newCondition();

public Account(String accountNo,double balance)

{

this.accountNo = accountNo;

this.balance = balance;

}

public double getBalance()

{

return balance;

}

public String getAccount()

{

return accountNo;

}

public synchronized void draw(double drawAmount)

{

lock.lock();

try {

//如果里面没钱,则等待(不能取钱)

if (!flag)

{

cond.await();

}

else {

//取钱

System.out.println(Thread.currentThread().getName()

+ "取钱成功:"+drawAmount);

balance -= drawAmount;

System.out.println("余额为"+balance);

//取钱之后,设置为不能取钱

flag = false;

cond.signalAll();

}

} catch (InterruptedException e) {

e.printStackTrace();

}

finally

{

lock.unlock();

}

}

public synchronized void deposit(double depositAmount)

{

lock.lock();

try

{

if(flag)

{

//flag为true表示已经有人存钱,则等待(不能存钱)

cond.await();

}

else {

//可以存钱

System.out.println(Thread.currentThread().getName()

+"存钱成功"+depositAmount);

balance += depositAmount;

flag = true;

System.out.println("账户余额为:"+balance);

cond.signalAll();

}

}

catch(InterruptedException e)

{

e.printStackTrace();

}

finally{lock.unlock();}

}

public int hashCode()

{

return accountNo.hashCode();

}

//判断是否为同一个对象

public boolean equals(Object obj)

{

if (obj != null && obj.getClass() == Account.class)

{

Account target = (Account) obj;

return target.getAccount().equals(accountNo);

}

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值