mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 16:14:58 +00:00
refactoring: reduce logs (#2370)
* feat: reduce logs from monitor pattern during test execution * fix: wait until all threads are done
This commit is contained in:
1 parent
18b04db103
commit
7a3d617769
4 files changed
+66
-16
No files matched your search
@@ -78,12 +78,14 @@ public class Bank {
|
|||||||
if (accounts[accountA] >= amount) {
|
if (accounts[accountA] >= amount) {
|
||||||
accounts[accountB] += amount;
|
accounts[accountB] += amount;
|
||||||
accounts[accountA] -= amount;
|
accounts[accountA] -= amount;
|
||||||
LOGGER.info(
|
if (LOGGER.isDebugEnabled()) {
|
||||||
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
|
LOGGER.debug(
|
||||||
accountA,
|
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
|
||||||
accountB,
|
accountA,
|
||||||
amount,
|
accountB,
|
||||||
getBalance());
|
amount,
|
||||||
|
getBalance());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
package com.iluwatar.monitor;
|
package com.iluwatar.monitor;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.concurrent.ExecutorService;
|
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;
|
||||||
|
|
||||||
@@ -40,20 +40,26 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
private static final int NUMBER_OF_THREADS = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runner to perform a bunch of transfers and handle exception.
|
* Runner to perform a bunch of transfers and handle exception.
|
||||||
*
|
*
|
||||||
* @param bank bank object
|
* @param bank bank object
|
||||||
|
* @param latch signal finished execution
|
||||||
*/
|
*/
|
||||||
public static void runner(Bank bank) {
|
public static void runner(Bank bank, CountDownLatch latch) {
|
||||||
try {
|
try {
|
||||||
SecureRandom random = new SecureRandom();
|
SecureRandom random = new SecureRandom();
|
||||||
Thread.sleep(random.nextInt(1000));
|
Thread.sleep(random.nextInt(1000));
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
LOGGER.info("Finished transferring.");
|
||||||
|
latch.countDown();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.info(e.getMessage());
|
LOGGER.error(e.getMessage());
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,12 +69,15 @@ public class Main {
|
|||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
var bank = new Bank(4, 1000);
|
var bank = new Bank(4, 1000);
|
||||||
Runnable runnable = () -> runner(bank);
|
var latch = new CountDownLatch(NUMBER_OF_THREADS);
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
executorService.execute(runnable);
|
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
|
||||||
|
executorService.execute(() -> runner(bank, latch));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
latch.await();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
package com.iluwatar.monitor;
|
package com.iluwatar.monitor;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
/** Test if the application starts without throwing an exception. */
|
/** Test if the application starts without throwing an exception. */
|
||||||
@@ -38,6 +39,9 @@ class MainTest {
|
|||||||
@Test
|
@Test
|
||||||
void RunnerExecuteWithoutException() {
|
void RunnerExecuteWithoutException() {
|
||||||
var bank = new Bank(4, 1000);
|
var bank = new Bank(4, 1000);
|
||||||
assertDoesNotThrow(() -> Main.runner(bank));
|
var latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> Main.runner(bank, latch));
|
||||||
|
assertEquals(0, latch.getCount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright © 2014-2022 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
Reference in new issue
Block a user