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-21 02:04:43 +08: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 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));
}
}
}
@@ -102,6 +104,16 @@ public class Bank {
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.
*
@@ -28,7 +28,6 @@ import java.security.SecureRandom;
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.
*
@@ -41,6 +40,8 @@ import lombok.extern.slf4j.Slf4j;
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.
@@ -54,7 +55,7 @@ public class Main {
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();
@@ -70,7 +71,7 @@ public class Main {
* @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);
@@ -69,4 +69,11 @@ class BankTest {
void BalanceHaveToBeOK() {
assertEquals(4000, bank.getBalance());
}
@Test
void ReturnBalanceWhenGivenAccountNumber() {
bank.transfer(0, 1, 1000);
assertEquals(0, bank.getBalance(0));
assertEquals(2000, bank.getBalance(1));
}
}