refactor: Refactor monitor patterns example code (#2560)

* feat:adjust the interval of amount because it is not reasonable before and add condition when transfer TianLeZhou 9 minutes ago

* feat:add ReturnBalanceWhenGivenAccountNumber test

* feat:adjust order of import package
This commit is contained in:
bakazhou
2023-08-20 21:04:43 +03:00
committed by GitHub
parent becedc12ef
commit 24090e47d0
3 changed files with 27 additions and 7 deletions
@@ -72,19 +72,21 @@ public class Bank {
* *
* @param accountA - source account * @param accountA - source account
* @param accountB - destination 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) { public synchronized void transfer(int accountA, int accountB, int amount) {
if (accounts[accountA] >= amount) { if (accounts[accountA] >= amount && accountA != accountB) {
accounts[accountB] += amount; accounts[accountB] += amount;
accounts[accountA] -= amount; accounts[accountA] -= amount;
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug( 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, accountA,
accountB, accountB,
amount, amount,
getBalance()); getBalance(),
getBalance(accountA),
getBalance(accountB));
} }
} }
} }
@@ -102,6 +104,16 @@ public class Bank {
return balance; 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. * Get all accounts.
* *
@@ -28,7 +28,6 @@ import java.security.SecureRandom;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
* The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion. * The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.
* *
@@ -41,6 +40,8 @@ import lombok.extern.slf4j.Slf4j;
public class Main { public class Main {
private static final int NUMBER_OF_THREADS = 5; 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. * Runner to perform a bunch of transfers and handle exception.
@@ -54,7 +55,7 @@ public class Main {
Thread.sleep(random.nextInt(1000)); Thread.sleep(random.nextInt(1000));
LOGGER.info("Start transferring..."); LOGGER.info("Start transferring...");
for (int i = 0; i < 1000000; i++) { 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."); LOGGER.info("Finished transferring.");
latch.countDown(); latch.countDown();
@@ -70,7 +71,7 @@ public class Main {
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) throws InterruptedException { 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 latch = new CountDownLatch(NUMBER_OF_THREADS);
var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS); var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
@@ -69,4 +69,11 @@ class BankTest {
void BalanceHaveToBeOK() { void BalanceHaveToBeOK() {
assertEquals(4000, bank.getBalance()); assertEquals(4000, bank.getBalance());
} }
@Test
void ReturnBalanceWhenGivenAccountNumber() {
bank.transfer(0, 1, 1000);
assertEquals(0, bank.getBalance(0));
assertEquals(2000, bank.getBalance(1));
}
} }