Skip to content

Commit faae2bb

Browse files
committed
[JAVA-9019] Logging clean up
1 parent 11d3896 commit faae2bb

6 files changed

Lines changed: 69 additions & 52 deletions

File tree

core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.baeldung.concurrent.prioritytaskexecution;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
public class Job implements Runnable {
4-
private String jobName;
5-
private JobPriority jobPriority;
6-
7+
8+
private static final Logger LOGGER = LoggerFactory.getLogger(Job.class);
9+
10+
private final String jobName;
11+
private final JobPriority jobPriority;
12+
713
public Job(String jobName, JobPriority jobPriority) {
814
this.jobName = jobName;
915
this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM;
@@ -16,8 +22,7 @@ public JobPriority getJobPriority() {
1622
@Override
1723
public void run() {
1824
try {
19-
System.out.println("Job:" + jobName +
20-
" Priority:" + jobPriority);
25+
LOGGER.debug("Job:{} Priority:{}", jobName, jobPriority);
2126
Thread.sleep(1000);
2227
} catch (InterruptedException ignored) {
2328
}

core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.baeldung.forkjoin;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.util.ArrayList;
47
import java.util.Collection;
58
import java.util.List;
69
import java.util.concurrent.ForkJoinTask;
710
import java.util.concurrent.RecursiveAction;
8-
import java.util.logging.Logger;
911

1012
public class CustomRecursiveAction extends RecursiveAction {
1113

14+
final Logger logger = LoggerFactory.getLogger(CustomRecursiveAction.class);
15+
1216
private String workLoad = "";
1317
private static final int THRESHOLD = 4;
1418

15-
private static Logger logger = Logger.getAnonymousLogger();
16-
1719
public CustomRecursiveAction(String workLoad) {
1820
this.workLoad = workLoad;
1921
}
@@ -43,7 +45,7 @@ private Collection<CustomRecursiveAction> createSubtasks() {
4345

4446
private void processing(String work) {
4547
String result = work.toUpperCase();
46-
logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread()
48+
logger.debug("This result - (" + result + ") - was processed by " + Thread.currentThread()
4749
.getName());
4850
}
4951
}

core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.junit.Test;
44

55
public class PriorityJobSchedulerUnitTest {
6-
private static int POOL_SIZE = 1;
7-
private static int QUEUE_SIZE = 10;
8-
6+
private static final int POOL_SIZE = 1;
7+
private static final int QUEUE_SIZE = 10;
8+
99
@Test
1010
public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
1111
Job job1 = new Job("Job1", JobPriority.LOW);
@@ -14,27 +14,27 @@ public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
1414
Job job4 = new Job("Job4", JobPriority.MEDIUM);
1515
Job job5 = new Job("Job5", JobPriority.LOW);
1616
Job job6 = new Job("Job6", JobPriority.HIGH);
17-
17+
1818
PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
19-
19+
2020
pjs.scheduleJob(job1);
2121
pjs.scheduleJob(job2);
2222
pjs.scheduleJob(job3);
2323
pjs.scheduleJob(job4);
2424
pjs.scheduleJob(job5);
2525
pjs.scheduleJob(job6);
26-
26+
2727
// ensure no tasks is pending before closing the scheduler
2828
while (pjs.getQueuedTaskCount() != 0);
29-
29+
3030
// delay to avoid job sleep (added for demo) being interrupted
3131
try {
3232
Thread.sleep(2000);
3333
} catch (InterruptedException e) {
3434
Thread.currentThread().interrupt();
3535
throw new RuntimeException(e);
3636
}
37-
37+
3838
pjs.closeScheduler();
3939
}
4040
}

core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.baeldung.forkjoin;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.assertTrue;
3+
import com.baeldung.forkjoin.util.PoolUtil;
4+
import org.junit.Before;
5+
import org.junit.Test;
66

77
import java.util.Random;
88
import java.util.concurrent.ForkJoinPool;
99

10-
import org.junit.Before;
11-
import org.junit.Test;
12-
13-
import com.baeldung.forkjoin.CustomRecursiveAction;
14-
import com.baeldung.forkjoin.CustomRecursiveTask;
15-
import com.baeldung.forkjoin.util.PoolUtil;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertTrue;
1613

1714
public class Java8ForkJoinIntegrationTest {
1815

@@ -63,11 +60,11 @@ public void executeRecursiveTask_whenExecuted_thenCorrect() {
6360
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
6461

6562
forkJoinPool.execute(customRecursiveTask);
66-
int result = customRecursiveTask.join();
63+
customRecursiveTask.join();
6764
assertTrue(customRecursiveTask.isDone());
6865

6966
forkJoinPool.submit(customRecursiveTask);
70-
int resultTwo = customRecursiveTask.join();
67+
customRecursiveTask.join();
7168
assertTrue(customRecursiveTask.isDone());
7269
}
7370

core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
package com.baeldung.thread.join;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
5-
6-
import java.util.logging.Logger;
7-
83
import org.junit.Ignore;
94
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertTrue;
1010

1111
/**
1212
* Demonstrates Thread.join behavior.
1313
*
1414
*/
1515
public class ThreadJoinUnitTest {
1616

17-
final static Logger LOGGER = Logger.getLogger(ThreadJoinUnitTest.class.getName());
17+
private static final Logger LOGGER = LoggerFactory.getLogger(ThreadJoinUnitTest.class);
1818

19-
class SampleThread extends Thread {
20-
public int processingCount = 0;
19+
static class SampleThread extends Thread {
20+
public int processingCount;
2121

2222
SampleThread(int processingCount) {
2323
this.processingCount = processingCount;
24-
LOGGER.info("Thread " + this.getName() + " created");
24+
LOGGER.debug("Thread " + this.getName() + " created");
2525
}
2626

2727
@Override
2828
public void run() {
29-
LOGGER.info("Thread " + this.getName() + " started");
29+
LOGGER.debug("Thread " + this.getName() + " started");
3030
while (processingCount > 0) {
3131
try {
3232
Thread.sleep(1000); // Simulate some work being done by thread
3333
} catch (InterruptedException e) {
34-
LOGGER.info("Thread " + this.getName() + " interrupted.");
34+
LOGGER.debug("Thread " + this.getName() + " interrupted.");
3535
}
3636
processingCount--;
37-
LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
37+
LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
3838
}
39-
LOGGER.info("Thread " + this.getName() + " exiting");
39+
LOGGER.debug("Thread " + this.getName() + " exiting");
4040
}
4141
}
4242

4343
@Test
4444
public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException {
4545
Thread t1 = new SampleThread(0);
46-
LOGGER.info("Invoking join.");
46+
LOGGER.debug("Invoking join.");
4747
t1.join();
48-
LOGGER.info("Returned from join");
49-
LOGGER.info("Thread state is" + t1.getState());
48+
LOGGER.debug("Returned from join");
49+
LOGGER.debug("Thread state is" + t1.getState());
5050
assertFalse(t1.isAlive());
5151
}
5252

5353
@Test
54-
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
54+
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
5555
throws InterruptedException {
5656
Thread t2 = new SampleThread(1);
5757
t2.start();
58-
LOGGER.info("Invoking join.");
58+
LOGGER.debug("Invoking join.");
5959
t2.join();
60-
LOGGER.info("Returned from join");
60+
LOGGER.debug("Returned from join");
6161
assertFalse(t2.isAlive());
6262
}
6363

6464
@Test
65-
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
65+
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
6666
throws InterruptedException {
6767
Thread t3 = new SampleThread(10);
6868
t3.start();
@@ -72,18 +72,18 @@ public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
7272

7373
@Test
7474
@Ignore
75-
public void givenThreadTerminated_checkForEffect_notGuaranteed()
75+
public void givenThreadTerminated_checkForEffect_notGuaranteed()
7676
throws InterruptedException {
7777
SampleThread t4 = new SampleThread(10);
7878
t4.start();
7979
//not guaranteed to stop even if t4 finishes.
8080
do {
81-
82-
} while (t4.processingCount > 0);
81+
82+
} while (t4.processingCount > 0);
8383
}
8484

8585
@Test
86-
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
86+
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
8787
throws InterruptedException {
8888
SampleThread t4 = new SampleThread(10);
8989
t4.start();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT"/>
12+
</root>
13+
</configuration>

0 commit comments

Comments
 (0)