The ThreadPoolExecutor constructor accepts the BlockingQueue parameter, which is the Queue implementation used to store pending jobs. You can request this queue using the getQueue() method, and then check the size of the queue:
System.out.println("Number of waiting jobs: "+executor.getQueue().size());
Please note that this method is not available in the ExecutorService interface, therefore it is better to build ThreadPoolExecutor explicitly instead of using Executors.newFixedThreadPool and friends:
ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
While Executors.newFixedThreadPool in OpenJDK / OracleJDK does the same, it is not specified, so using (ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads) may lead to a ClassCastException in future versions of Java or alternative JDK implementations.
source share