我试图运行ExecutorService
对象,FixedThreadPool
但遇到了问题。
我希望程序能在纳秒内运行,但它被挂起。我发现我需要Semaphore
和它一起使用,这样队列中的项目就不会被累加。
有什么方法可以让我知道池中的所有线程都已使用。
基本代码...
static ExecutorService pool = Executors.newFixedThreadPool(4);
static Semaphore permits = new Semaphore(4);
try {
permits.acquire();
pool.execute(p); // Assuming p is runnable on large number of objects
permits.release();
} catch ( InterruptedException ex ) {
}
这段代码被挂起,我真的不知道为什么。如何知道池当前是否正在等待所有线程完成?