Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.Phaser; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; public class PhaserAwaitAdvanceInterruptiblyExample3 { public static void main(String[] args) throws InterruptedException, TimeoutException { Phaser p = new Phaser(10); p.register(); Phaser pc = p.getParent(); System.out.println("Thread is sleeping"); Thread.sleep(5000); for(int i=0; i<=5; i++) { new PhaserAwaitAdvanceInterruptiblyExample1().test(p); } } void test(final Phaser p) { new Thread(){ @Override public void run() { System.out.println(Thread.currentThread().getName()+" arrived" ); boolean wasTe = false; try { p.awaitAdvanceInterruptibly(0, 2, TimeUnit.SECONDS); } catch (TimeoutException te) { wasTe = true; } catch (InterruptedException ex) { Logger.getLogger(PhaserAwaitAdvanceInterruptiblyExample1.class.getName()).log(Level.SEVERE, null, ex); } } }.start(); } }
Output