ExecutorService是个接口,如果你是用Executors静态方法生产的实例,见具体实现。
比如:
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue
threadFactory);
}
那它是ThreadPoolExecutor的实例,你可以看它的方法,如getActiveCount(),getPoolSize()等。
找了半天我没找到答案,ExecutorService是接口,ThreadPoolExecutor继承AbstractExecutorService实现了ExecutorService,是真正的线程池,然而ThreadPoolExecutor里也并没有提供查看空余线程的方法,思考了下应该是这样,getPoolSize-getActiveCount,即当前池子大小减去正在活动的线程数量为当前空余线程。
ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(100);//创建线程池,这种线程池固定了线程数量
pool.getActiveCount();//获取活动的线程数量
100-活动数量就是空闲数量