📄 account.java
字号:
class Account{ int balance; String name; public Account(int amt, String nm) { balance = amt; name = nm; }/* The transfer() method is used to transfer money from the invoking *//* Account object to the one passed as an argument to the method. *//* The transfer() method is synchronized. It first subtracts the *//* specified amount from the invoking Account object and then adds *//* the same amount to the other Account object. The subtractBalance() *//* and addBalance() methods are synchronized. Calling a synchronized *//* method from within a synchronized method can lead to deadlock. */ public synchronized void transfer(Account acctB, int amount) { subtractBalance(amount); try { Thread.sleep(1000); } catch (Exception e) {} System.out.println(Thread.currentThread().getName()+ " trying to call addBalance()"); acctB.addBalance(amount);//调用另外一个帐户对象的同步方法可能引起死锁。 } public synchronized void addBalance(int amt) { balance += amt; } public synchronized void subtractBalance(int amt) { balance -= amt; } public int getBalance() { return balance; } public String getName() { return name; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -