Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor monitor patterns example code #2560

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions monitor/src/main/java/com/iluwatar/monitor/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,21 @@ public Bank(int accountNum, int baseAmount) {
*
* @param accountA - source account
* @param accountB - destination account
* @param amount - amount to be transferred
* @param amount - amount to be transferred
*/
public synchronized void transfer(int accountA, int accountB, int amount) {
if (accounts[accountA] >= amount) {
if (accounts[accountA] >= amount && accountA != accountB) {
accounts[accountB] += amount;
accounts[accountA] -= amount;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
"Transferred from account: {} to account: {} , amount: {} , bank balance at: {}, source account balance: {}, destination account balance: {}",
accountA,
accountB,
amount,
getBalance());
getBalance(),
getBalance(accountA),
getBalance(accountB));
}
}
}
Expand All @@ -102,6 +104,16 @@ public synchronized int getBalance() {
return balance;
}

/**
* Get the accountNumber balance.
*
* @param accountNumber - accountNumber number
* @return accounts[accountNumber]
*/
public synchronized int getBalance(int accountNumber) {
return accounts[accountNumber];
}

/**
* Get all accounts.
*
Expand Down
7 changes: 4 additions & 3 deletions monitor/src/main/java/com/iluwatar/monitor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;

/**
* The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.
*
Expand All @@ -41,6 +40,8 @@
public class Main {

private static final int NUMBER_OF_THREADS = 5;
private static final int BASE_AMOUNT = 1000;
private static final int ACCOUNT_NUM = 4;

/**
* Runner to perform a bunch of transfers and handle exception.
Expand All @@ -54,7 +55,7 @@ public static void runner(Bank bank, CountDownLatch latch) {
Thread.sleep(random.nextInt(1000));
LOGGER.info("Start transferring...");
for (int i = 0; i < 1000000; i++) {
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt());
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt(0, BASE_AMOUNT));
}
LOGGER.info("Finished transferring.");
latch.countDown();
Expand All @@ -70,7 +71,7 @@ public static void runner(Bank bank, CountDownLatch latch) {
* @param args command line args
*/
public static void main(String[] args) throws InterruptedException {
var bank = new Bank(4, 1000);
var bank = new Bank(ACCOUNT_NUM, BASE_AMOUNT);
var latch = new CountDownLatch(NUMBER_OF_THREADS);
var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);

Expand Down
7 changes: 7 additions & 0 deletions monitor/src/test/java/com/iluwatar/monitor/BankTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ void TransferMethodHaveToTransferAmountFromAnAccountToOtherAccount() {
void BalanceHaveToBeOK() {
assertEquals(4000, bank.getBalance());
}

@Test
void ReturnBalanceWhenGivenAccountNumber() {
bank.transfer(0, 1, 1000);
assertEquals(0, bank.getBalance(0));
assertEquals(2000, bank.getBalance(1));
}
}