[JAVA-13959] Alligned code with article (#12982)

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos 2022-11-06 18:33:13 +00:00 committed by GitHub
parent 198bf2181b
commit ca8e33e339

View File

@ -1,4 +1,4 @@
package com.baeldung.thread.join;
package com.baeldung.concurrent.threadjoin;
import org.junit.Ignore;
import org.junit.Test;
@ -21,49 +21,47 @@ public class ThreadJoinUnitTest {
SampleThread(int processingCount) {
this.processingCount = processingCount;
LOGGER.debug("Thread " + this.getName() + " created");
LOGGER.info("Thread " + this.getName() + " created");
}
@Override
public void run() {
LOGGER.debug("Thread " + this.getName() + " started");
LOGGER.info("Thread " + this.getName() + " started");
while (processingCount > 0) {
try {
Thread.sleep(1000); // Simulate some work being done by thread
} catch (InterruptedException e) {
LOGGER.debug("Thread " + this.getName() + " interrupted.");
LOGGER.info("Thread " + this.getName() + " interrupted.");
}
processingCount--;
LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
}
LOGGER.debug("Thread " + this.getName() + " exiting");
LOGGER.info("Thread " + this.getName() + " exiting");
}
}
@Test
public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException {
Thread t1 = new SampleThread(0);
LOGGER.debug("Invoking join.");
LOGGER.info("Invoking join");
t1.join();
LOGGER.debug("Returned from join");
LOGGER.debug("Thread state is" + t1.getState());
LOGGER.info("Returned from join");
LOGGER.info("Thread state is" + t1.getState());
assertFalse(t1.isAlive());
}
@Test
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
throws InterruptedException {
public void givenStartedThread_whenJoinCalled_waitsTillCompletion() throws InterruptedException {
Thread t2 = new SampleThread(1);
t2.start();
LOGGER.debug("Invoking join.");
LOGGER.info("Invoking join");
t2.join();
LOGGER.debug("Returned from join");
LOGGER.info("Returned from join");
assertFalse(t2.isAlive());
}
@Test
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
throws InterruptedException {
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() throws InterruptedException {
Thread t3 = new SampleThread(10);
t3.start();
t3.join(1000);
@ -72,19 +70,17 @@ public class ThreadJoinUnitTest {
@Test
@Ignore
public void givenThreadTerminated_checkForEffect_notGuaranteed()
throws InterruptedException {
public void givenThreadTerminated_checkForEffect_notGuaranteed() throws InterruptedException {
SampleThread t4 = new SampleThread(10);
t4.start();
//not guaranteed to stop even if t4 finishes.
//not guaranteed to stop even if t4 finishes.
do {
} while (t4.processingCount > 0);
}
@Test
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
throws InterruptedException {
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() throws InterruptedException {
SampleThread t4 = new SampleThread(10);
t4.start();
do {