Java tutorial
//package com.java2s; import java.util.List; import java.util.concurrent.TimeUnit; public class Main { public static void awaitForTermination(List<Thread> threads, long timeout, TimeUnit timeUnit) { try { long finishTime = System.currentTimeMillis() + timeUnit.toMillis(timeout); for (Thread t : threads) { long now = System.currentTimeMillis(); if (now > finishTime) { throw new IllegalStateException("failed to stop all threads"); } else { t.join(finishTime - now); if (t.isAlive()) { throw new IllegalStateException("failed to stop all threads"); } } } } catch (InterruptedException e) { throw new IllegalStateException(e); } } }