From f72971181ef5722619a60c927ee59439a05fa46b Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 28 Jan 2022 22:50:49 +0000 Subject: [PATCH] [JAVA-9019] Logging clean up --- .../concurrent/prioritytaskexecution/Job.java | 15 ++++-- .../forkjoin/CustomRecursiveAction.java | 10 ++-- .../PriorityJobSchedulerUnitTest.java | 16 +++---- .../Java8ForkJoinIntegrationTest.java | 19 ++++---- .../thread/join/ThreadJoinUnitTest.java | 48 +++++++++---------- .../src/test/resources/logback.xml | 13 +++++ 6 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java index 9900d1c63d..b0866cb7f0 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -1,9 +1,15 @@ package com.baeldung.concurrent.prioritytaskexecution; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class Job implements Runnable { - private String jobName; - private JobPriority jobPriority; - + + private static final Logger LOGGER = LoggerFactory.getLogger(Job.class); + + private final String jobName; + private final JobPriority jobPriority; + public Job(String jobName, JobPriority jobPriority) { this.jobName = jobName; this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM; @@ -16,8 +22,7 @@ public class Job implements Runnable { @Override public void run() { try { - System.out.println("Job:" + jobName + - " Priority:" + jobPriority); + LOGGER.debug("Job:{} Priority:{}", jobName, jobPriority); Thread.sleep(1000); } catch (InterruptedException ignored) { } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java index ebe59e33b1..26d620ba07 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java @@ -1,19 +1,21 @@ package com.baeldung.forkjoin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveAction; -import java.util.logging.Logger; public class CustomRecursiveAction extends RecursiveAction { + final Logger logger = LoggerFactory.getLogger(CustomRecursiveAction.class); + private String workLoad = ""; private static final int THRESHOLD = 4; - private static Logger logger = Logger.getAnonymousLogger(); - public CustomRecursiveAction(String workLoad) { this.workLoad = workLoad; } @@ -43,7 +45,7 @@ public class CustomRecursiveAction extends RecursiveAction { private void processing(String work) { String result = work.toUpperCase(); - logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread() + logger.debug("This result - (" + result + ") - was processed by " + Thread.currentThread() .getName()); } } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java index 1e67fe45c1..20e6a5ef5b 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -3,9 +3,9 @@ package com.baeldung.concurrent.prioritytaskexecution; import org.junit.Test; public class PriorityJobSchedulerUnitTest { - private static int POOL_SIZE = 1; - private static int QUEUE_SIZE = 10; - + private static final int POOL_SIZE = 1; + private static final int QUEUE_SIZE = 10; + @Test public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() { Job job1 = new Job("Job1", JobPriority.LOW); @@ -14,19 +14,19 @@ public class PriorityJobSchedulerUnitTest { Job job4 = new Job("Job4", JobPriority.MEDIUM); Job job5 = new Job("Job5", JobPriority.LOW); Job job6 = new Job("Job6", JobPriority.HIGH); - + PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE); - + pjs.scheduleJob(job1); pjs.scheduleJob(job2); pjs.scheduleJob(job3); pjs.scheduleJob(job4); pjs.scheduleJob(job5); pjs.scheduleJob(job6); - + // ensure no tasks is pending before closing the scheduler while (pjs.getQueuedTaskCount() != 0); - + // delay to avoid job sleep (added for demo) being interrupted try { Thread.sleep(2000); @@ -34,7 +34,7 @@ public class PriorityJobSchedulerUnitTest { Thread.currentThread().interrupt(); throw new RuntimeException(e); } - + pjs.closeScheduler(); } } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java index c469fe0195..ec10e2be79 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java @@ -1,18 +1,15 @@ package com.baeldung.forkjoin; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import com.baeldung.forkjoin.util.PoolUtil; +import org.junit.Before; +import org.junit.Test; import java.util.Random; import java.util.concurrent.ForkJoinPool; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.forkjoin.CustomRecursiveAction; -import com.baeldung.forkjoin.CustomRecursiveTask; -import com.baeldung.forkjoin.util.PoolUtil; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class Java8ForkJoinIntegrationTest { @@ -63,11 +60,11 @@ public class Java8ForkJoinIntegrationTest { ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); forkJoinPool.execute(customRecursiveTask); - int result = customRecursiveTask.join(); + customRecursiveTask.join(); assertTrue(customRecursiveTask.isDone()); forkJoinPool.submit(customRecursiveTask); - int resultTwo = customRecursiveTask.join(); + customRecursiveTask.join(); assertTrue(customRecursiveTask.isDone()); } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java index 85cff51eb3..dc30ce6c74 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java @@ -1,12 +1,12 @@ package com.baeldung.thread.join; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.logging.Logger; - import org.junit.Ignore; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Demonstrates Thread.join behavior. @@ -14,55 +14,55 @@ import org.junit.Test; */ public class ThreadJoinUnitTest { - final static Logger LOGGER = Logger.getLogger(ThreadJoinUnitTest.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(ThreadJoinUnitTest.class); - class SampleThread extends Thread { - public int processingCount = 0; + static class SampleThread extends Thread { + public int processingCount; SampleThread(int processingCount) { this.processingCount = processingCount; - LOGGER.info("Thread " + this.getName() + " created"); + LOGGER.debug("Thread " + this.getName() + " created"); } @Override public void run() { - LOGGER.info("Thread " + this.getName() + " started"); + LOGGER.debug("Thread " + this.getName() + " started"); while (processingCount > 0) { try { Thread.sleep(1000); // Simulate some work being done by thread } catch (InterruptedException e) { - LOGGER.info("Thread " + this.getName() + " interrupted."); + LOGGER.debug("Thread " + this.getName() + " interrupted."); } processingCount--; - LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount); + LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount); } - LOGGER.info("Thread " + this.getName() + " exiting"); + LOGGER.debug("Thread " + this.getName() + " exiting"); } } @Test public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException { Thread t1 = new SampleThread(0); - LOGGER.info("Invoking join."); + LOGGER.debug("Invoking join."); t1.join(); - LOGGER.info("Returned from join"); - LOGGER.info("Thread state is" + t1.getState()); + LOGGER.debug("Returned from join"); + LOGGER.debug("Thread state is" + t1.getState()); assertFalse(t1.isAlive()); } @Test - public void givenStartedThread_whenJoinCalled_waitsTillCompletion() + public void givenStartedThread_whenJoinCalled_waitsTillCompletion() throws InterruptedException { Thread t2 = new SampleThread(1); t2.start(); - LOGGER.info("Invoking join."); + LOGGER.debug("Invoking join."); t2.join(); - LOGGER.info("Returned from join"); + LOGGER.debug("Returned from join"); assertFalse(t2.isAlive()); } @Test - public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() + public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() throws InterruptedException { Thread t3 = new SampleThread(10); t3.start(); @@ -72,18 +72,18 @@ public class ThreadJoinUnitTest { @Test @Ignore - public void givenThreadTerminated_checkForEffect_notGuaranteed() + public void givenThreadTerminated_checkForEffect_notGuaranteed() throws InterruptedException { SampleThread t4 = new SampleThread(10); t4.start(); //not guaranteed to stop even if t4 finishes. do { - - } while (t4.processingCount > 0); + + } while (t4.processingCount > 0); } @Test - public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() + public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() throws InterruptedException { SampleThread t4 = new SampleThread(10); t4.start(); diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml b/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml new file mode 100644 index 0000000000..b2fa0488f3 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + +