Skip to main content

Practice ยท 3 of 3

Latch-Synchronized Fan-Out

Implement static List<String> fanOut(int n): - create a CountDownLatch(n) and a thread-safe list (Collections.synchronizedList or CopyOnWriteArrayList) - start n threads; each appends "t" + its_index to the list, then counts down the latch - the main thread awaits the latch, SORTS the list, and returns it Every returned list must contain exactly t0..t(n-1) โ€” the latch guarantees completion before the read. n <= 0 returns an empty list.

Difficulty: intermediate

Back to lesson: Practice: Concurrency Lab