JAVA-24259 | small refactor for readability (#14602)

This commit is contained in:
Gaetano Piazzolla 2023-08-23 17:37:47 +02:00 committed by GitHub
parent fad32db315
commit 1f98b51b8f
2 changed files with 32 additions and 34 deletions

View File

@ -7,31 +7,37 @@ import java.util.concurrent.Phaser;
class LongRunningAction implements Runnable {
private static Logger log = LoggerFactory.getLogger(LongRunningAction.class);
private String threadName;
private Phaser ph;
private static final Logger log = LoggerFactory.getLogger(LongRunningAction.class);
private final String threadName;
private final Phaser ph;
LongRunningAction(String threadName, Phaser ph) {
this.threadName = threadName;
this.ph = ph;
this.randomWait();
ph.register();
log.info("Thread {} registered during phase {}", threadName, ph.getPhase());
}
@Override
public void run() {
log.info("This is phase {}", ph.getPhase());
log.info("Thread {} before long running action", threadName);
log.info("Thread {} BEFORE long running action in phase {}", threadName, ph.getPhase());
ph.arriveAndAwaitAdvance();
randomWait();
log.info("Thread {} AFTER long running action in phase {}", threadName, ph.getPhase());
ph.arriveAndDeregister();
}
// Simulating real work
private void randomWait() {
try {
Thread.sleep(2000);
Thread.sleep((long) (Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("Thread {} action completed and waiting for others", threadName);
ph.arriveAndAwaitAdvance();
log.debug("Thread {} proceeding in phase {}", threadName, ph.getPhase());
ph.arriveAndDeregister();
}
}

View File

@ -7,8 +7,6 @@ import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser;
import static junit.framework.TestCase.assertEquals;
@ -16,38 +14,32 @@ import static junit.framework.TestCase.assertEquals;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PhaserUnitTest {
private static Logger log = LoggerFactory.getLogger(PhaserUnitTest.class);
private static final Logger log = LoggerFactory.getLogger(PhaserUnitTest.class);
@Test
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() {
//given
ExecutorService executorService = Executors.newCachedThreadPool();
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() throws InterruptedException {
Phaser ph = new Phaser(1);
assertEquals(0, ph.getPhase());
//when
executorService.submit(new LongRunningAction("thread-1", ph));
executorService.submit(new LongRunningAction("thread-2", ph));
executorService.submit(new LongRunningAction("thread-3", ph));
new Thread(new LongRunningAction("thread-1", ph)).start();
new Thread(new LongRunningAction("thread-2", ph)).start();
new Thread(new LongRunningAction("thread-3", ph)).start();
//then
log.debug("Thread {} waiting for others", Thread.currentThread().getName());
log.info("Thread {} waiting for others", Thread.currentThread().getName());
ph.arriveAndAwaitAdvance();
log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
log.info("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
assertEquals(1, ph.getPhase());
//and
executorService.submit(new LongRunningAction("thread-4", ph));
executorService.submit(new LongRunningAction("thread-5", ph));
new Thread(new LongRunningAction("thread-4", ph)).start();
new Thread(new LongRunningAction("thread-5", ph)).start();
log.debug("Thread {} waiting for others", Thread.currentThread().getName());
log.info("Thread {} waiting for new phase", Thread.currentThread().getName());
ph.arriveAndAwaitAdvance();
log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
log.info("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
assertEquals(2, ph.getPhase());
ph.arriveAndDeregister();
Thread.sleep(1000);
assertEquals(true, ph.isTerminated());
}
}