Java SE API
Phaser.awaitAdvanceInterruptibly(int, long, TimeUnit)
Wait for phase advancement or termination with interruption and timeout.
Formal Syntax & Structure
java syntaxjava
int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit) throws InterruptedException, TimeoutExceptionParameters & Attributes
| Name | Type | Description |
|---|---|---|
| timeout | long | Maximum waiting time |
| unit | TimeUnit | Time unit for the supplied duration |
| phase | int | Phase to wait beyond |
Working Example
Example Snippetjava
import java.util.concurrent.*;import java.util.concurrent.atomic.*;import java.util.concurrent.locks.*;public class Main { static ThreadPoolExecutor pool(int workers, int capacity) { return new ThreadPoolExecutor(workers, workers, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(capacity), new ThreadPoolExecutor.AbortPolicy()); } static void stop(ExecutorService pool) throws InterruptedException { pool.shutdown(); try { if (!pool.awaitTermination(3, TimeUnit.SECONDS)) { for (Runnable pending : pool.shutdownNow()) { if (pending instanceof Future<?> future) future.cancel(false); } if (!pool.awaitTermination(3, TimeUnit.SECONDS)) throw new AssertionError("workers still running"); } } catch (InterruptedException e) { for (Runnable pending : pool.shutdownNow()) { if (pending instanceof Future<?> future) future.cancel(false); } throw e; } } public static void main(String[] args) throws Exception { Phaser phase = new Phaser(2); ThreadPoolExecutor workers = pool(1, 1); try { Future<Integer> worker = workers.submit(() -> phase.awaitAdvanceInterruptibly(phase.arrive(), 3, TimeUnit.SECONDS)); int next = phase.awaitAdvanceInterruptibly(phase.arrive(), 3, TimeUnit.SECONDS); System.out.println(next); System.out.println(worker.get(3, TimeUnit.SECONDS)); } finally { phase.forceTermination(); stop(workers); } }}