Last active
August 22, 2020 17:26
-
-
Save eaiman-shoshi/c98329e8d1502c73ddb81a6d9f4f3786 to your computer and use it in GitHub Desktop.
bla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.masterdevskills.cha1; | |
public class Account { | |
private int balance; | |
public Account(int balance) { | |
this.balance = balance; | |
} | |
public void transfer (int value, Account account) { | |
synchronized (account) { | |
synchronized (this) { | |
balance = balance - value; | |
account.deposit(value); | |
} | |
} | |
} | |
public void deposit(int value) { | |
balance = balance + value; | |
} | |
public static void main(String[] args) { | |
Account a = new Account(100); | |
Account b = new Account(500); | |
a.transfer(50, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment