JAVA-24259 | small refactor for readability (#14602)
This commit is contained in:
parent
fad32db315
commit
1f98b51b8f
|
@ -7,31 +7,37 @@ import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
class LongRunningAction implements Runnable {
|
class LongRunningAction implements Runnable {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(LongRunningAction.class);
|
private static final Logger log = LoggerFactory.getLogger(LongRunningAction.class);
|
||||||
private String threadName;
|
private final String threadName;
|
||||||
private Phaser ph;
|
private final Phaser ph;
|
||||||
|
|
||||||
LongRunningAction(String threadName, Phaser ph) {
|
LongRunningAction(String threadName, Phaser ph) {
|
||||||
this.threadName = threadName;
|
this.threadName = threadName;
|
||||||
this.ph = ph;
|
this.ph = ph;
|
||||||
|
|
||||||
|
this.randomWait();
|
||||||
|
|
||||||
ph.register();
|
ph.register();
|
||||||
|
log.info("Thread {} registered during phase {}", threadName, ph.getPhase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
log.info("This is phase {}", ph.getPhase());
|
log.info("Thread {} BEFORE long running action in phase {}", threadName, ph.getPhase());
|
||||||
log.info("Thread {} before long running action", threadName);
|
ph.arriveAndAwaitAdvance();
|
||||||
|
|
||||||
|
randomWait();
|
||||||
|
|
||||||
|
log.info("Thread {} AFTER long running action in phase {}", threadName, ph.getPhase());
|
||||||
|
ph.arriveAndDeregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simulating real work
|
||||||
|
private void randomWait() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(2000);
|
Thread.sleep((long) (Math.random() * 100));
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,8 +7,6 @@ import org.junit.runners.MethodSorters;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Phaser;
|
import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
@ -16,38 +14,32 @@ import static junit.framework.TestCase.assertEquals;
|
||||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
public class PhaserUnitTest {
|
public class PhaserUnitTest {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(PhaserUnitTest.class);
|
private static final Logger log = LoggerFactory.getLogger(PhaserUnitTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() {
|
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() throws InterruptedException {
|
||||||
//given
|
|
||||||
ExecutorService executorService = Executors.newCachedThreadPool();
|
|
||||||
Phaser ph = new Phaser(1);
|
Phaser ph = new Phaser(1);
|
||||||
assertEquals(0, ph.getPhase());
|
assertEquals(0, ph.getPhase());
|
||||||
|
|
||||||
//when
|
new Thread(new LongRunningAction("thread-1", ph)).start();
|
||||||
executorService.submit(new LongRunningAction("thread-1", ph));
|
new Thread(new LongRunningAction("thread-2", ph)).start();
|
||||||
executorService.submit(new LongRunningAction("thread-2", ph));
|
new Thread(new LongRunningAction("thread-3", ph)).start();
|
||||||
executorService.submit(new LongRunningAction("thread-3", ph));
|
|
||||||
|
|
||||||
//then
|
log.info("Thread {} waiting for others", Thread.currentThread().getName());
|
||||||
log.debug("Thread {} waiting for others", Thread.currentThread().getName());
|
|
||||||
ph.arriveAndAwaitAdvance();
|
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());
|
assertEquals(1, ph.getPhase());
|
||||||
|
|
||||||
//and
|
new Thread(new LongRunningAction("thread-4", ph)).start();
|
||||||
executorService.submit(new LongRunningAction("thread-4", ph));
|
new Thread(new LongRunningAction("thread-5", ph)).start();
|
||||||
executorService.submit(new LongRunningAction("thread-5", ph));
|
|
||||||
|
|
||||||
log.debug("Thread {} waiting for others", Thread.currentThread().getName());
|
log.info("Thread {} waiting for new phase", Thread.currentThread().getName());
|
||||||
ph.arriveAndAwaitAdvance();
|
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());
|
assertEquals(2, ph.getPhase());
|
||||||
|
|
||||||
|
|
||||||
ph.arriveAndDeregister();
|
ph.arriveAndDeregister();
|
||||||
|
Thread.sleep(1000);
|
||||||
|
assertEquals(true, ph.isTerminated());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue