Move arrive and wait after thread sleep and detailed logging

This commit is contained in:
mobin 2022-01-15 16:37:28 +05:30
parent e29186f270
commit 31ddc6a45d
2 changed files with 14 additions and 2 deletions

View File

@ -16,12 +16,17 @@ class LongRunningAction implements Runnable {
public void run() { public void run() {
System.out.println("This is phase " + ph.getPhase()); System.out.println("This is phase " + ph.getPhase());
System.out.println("Thread " + threadName + " before long running action"); System.out.println("Thread " + threadName + " before long running action");
ph.arriveAndAwaitAdvance();
try { try {
Thread.sleep(20); Thread.sleep(2000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Thread " + threadName + " action completed and waiting for others");
ph.arriveAndAwaitAdvance();
System.out.println("Thread " + threadName + " proceeding in phase " + ph.getPhase());
ph.arriveAndDeregister(); ph.arriveAndDeregister();
} }
} }

View File

@ -26,13 +26,20 @@ public class PhaserUnitTest {
executorService.submit(new LongRunningAction("thread-3", ph)); executorService.submit(new LongRunningAction("thread-3", ph));
//then //then
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
ph.arriveAndAwaitAdvance(); ph.arriveAndAwaitAdvance();
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
assertEquals(1, ph.getPhase()); assertEquals(1, ph.getPhase());
//and //and
executorService.submit(new LongRunningAction("thread-4", ph)); executorService.submit(new LongRunningAction("thread-4", ph));
executorService.submit(new LongRunningAction("thread-5", ph)); executorService.submit(new LongRunningAction("thread-5", ph));
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others");
ph.arriveAndAwaitAdvance(); ph.arriveAndAwaitAdvance();
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase());
assertEquals(2, ph.getPhase()); assertEquals(2, ph.getPhase());