Skip to main content

Practice ยท 1 of 3

Executor lifecycle, executed

Implement static List<String> lifecycle() โ€” choreography over a newFixedThreadPool(1) with CountDownLatch start/proceed/blocked and a CopyOnWriteArrayList<String> log: 1. Submit task A: log "A-running"; blocked.countDown(); proceed.await() โ€” if interrupted log "A-interrupted" and return; otherwise log "A-done". 2. Main: blocked.await() (A is parked; the only thread is busy), then submit task B that logs "B-done" โ€” B is now QUEUED. 3. shutdown(). 4. Try submitting task C; catch RejectedExecutionException and log "rejected-after-shutdown". 5. proceed.countDown(); awaitTermination(5, SECONDS); return the log. With shutdown(), queued B must still run: [A-running, rejected-after-shutdown, A-done, B-done].

Difficulty: advanced

Back to lesson: Practice: Executor & VT drills