Skip to content

Commit 7a3d617

Browse files
refactoring: reduce logs (iluwatar#2370)
* feat: reduce logs from monitor pattern during test execution * fix: wait until all threads are done
1 parent 18b04db commit 7a3d617

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

monitor/src/main/java/com/iluwatar/monitor/Bank.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ public synchronized void transfer(int accountA, int accountB, int amount) {
7878
if (accounts[accountA] >= amount) {
7979
accounts[accountB] += amount;
8080
accounts[accountA] -= amount;
81-
LOGGER.info(
82-
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
83-
accountA,
84-
accountB,
85-
amount,
86-
getBalance());
81+
if (LOGGER.isDebugEnabled()) {
82+
LOGGER.debug(
83+
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
84+
accountA,
85+
accountB,
86+
amount,
87+
getBalance());
88+
}
8789
}
8890
}
8991

monitor/src/main/java/com/iluwatar/monitor/Main.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package com.iluwatar.monitor;
2626

2727
import java.security.SecureRandom;
28-
import java.util.concurrent.ExecutorService;
28+
import java.util.concurrent.CountDownLatch;
2929
import java.util.concurrent.Executors;
3030
import lombok.extern.slf4j.Slf4j;
3131

@@ -40,20 +40,26 @@
4040
@Slf4j
4141
public class Main {
4242

43+
private static final int NUMBER_OF_THREADS = 5;
44+
4345
/**
4446
* Runner to perform a bunch of transfers and handle exception.
4547
*
46-
* @param bank bank object
48+
* @param bank bank object
49+
* @param latch signal finished execution
4750
*/
48-
public static void runner(Bank bank) {
51+
public static void runner(Bank bank, CountDownLatch latch) {
4952
try {
5053
SecureRandom random = new SecureRandom();
5154
Thread.sleep(random.nextInt(1000));
55+
LOGGER.info("Start transferring...");
5256
for (int i = 0; i < 1000000; i++) {
5357
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt());
5458
}
59+
LOGGER.info("Finished transferring.");
60+
latch.countDown();
5561
} catch (InterruptedException e) {
56-
LOGGER.info(e.getMessage());
62+
LOGGER.error(e.getMessage());
5763
Thread.currentThread().interrupt();
5864
}
5965
}
@@ -63,12 +69,15 @@ public static void runner(Bank bank) {
6369
*
6470
* @param args command line args
6571
*/
66-
public static void main(String[] args) {
72+
public static void main(String[] args) throws InterruptedException {
6773
var bank = new Bank(4, 1000);
68-
Runnable runnable = () -> runner(bank);
69-
ExecutorService executorService = Executors.newFixedThreadPool(5);
70-
for (int i = 0; i < 5; i++) {
71-
executorService.execute(runnable);
74+
var latch = new CountDownLatch(NUMBER_OF_THREADS);
75+
var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
76+
77+
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
78+
executorService.execute(() -> runner(bank, latch));
7279
}
80+
81+
latch.await();
7382
}
7483
}

monitor/src/test/java/com/iluwatar/monitor/MainTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.iluwatar.monitor;
2626

2727
import org.junit.jupiter.api.Test;
28+
import java.util.concurrent.CountDownLatch;
2829
import static org.junit.jupiter.api.Assertions.*;
2930

3031
/** Test if the application starts without throwing an exception. */
@@ -38,6 +39,9 @@ void shouldExecuteApplicationWithoutException() {
3839
@Test
3940
void RunnerExecuteWithoutException() {
4041
var bank = new Bank(4, 1000);
41-
assertDoesNotThrow(() -> Main.runner(bank));
42+
var latch = new CountDownLatch(1);
43+
44+
assertDoesNotThrow(() -> Main.runner(bank, latch));
45+
assertEquals(0, latch.getCount());
4246
}
4347
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2022 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<configuration>
27+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
28+
<encoder>
29+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
30+
</encoder>
31+
</appender>
32+
<root level="info">
33+
<appender-ref ref="STDOUT" />
34+
</root>
35+
</configuration>

0 commit comments

Comments
 (0)