听起来像一个线程池会很好.基本上,你掀起了N个不同线程的集合,然后循环请求它们.请求将阻塞,直到线程可用.
ThreadPool pool = Executors.newFixedThreadPool(10); // 10 threads in the pool
ArrayList collectionOfCallables = new ArrayList( );
for (...) {
Callable callable = new Callable() { public Foo call() { COMPUTE INTENSIVE SECTION } }
collectionOfCallables.add(callable);
}
ArrayList> results = pool.invokeAll( collectionOfCallables );
pool.awaitTermination(5, TimeUnit.MINUTES ); // blocks till everything is done or 5 minutes have passed.
有了Future,你真的不需要等待终止. get()来自未来的结果将阻塞,直到相应的线程完成(或取消).