ExecutorService中的活动线程
任何想法如何确定在ExecutorService中运行的活动线程的数量?
使用ThreadPoolExecutor实现并调用getActiveCount() :
int getActiveCount() // Returns the approximate number of threads that are actively executing tasks.
ExecutorService接口不提供一个方法,这取决于实现。
检查Executors.newFixedThreadPool()的源代码:
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
ThreadPoolExecutor有一个getActiveCount()方法。 所以你可以将ExecutorService转换为ThreadPoolExecutor,或者直接使用上面的代码来获得一个。 然后你可以调用getActiveCount()。
假定pool
是ExecutorService实例的名称:
if (pool instanceof ThreadPoolExecutor) { System.out.println( "Pool size is now " + ((ThreadPoolExecutor) pool).getActiveCount() ); }
ExecutorService接口没有定义一个方法来检查池中工作线程的数量,因为这是一个实现细节
public int getPoolSize() Returns the current number of threads in the pool.
在ThreadPoolExecutor类中可用
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; 公共类PoolSize { public static void main(String [] args){ ThreadPoolExecutor executor = new ThreadPoolExecutor(10,20,60L,TimeUnit.SECONDS,new LinkedBlockingQueue()); 的System.out.println(executor.getPoolSize()); } }
但是这需要你显式地创buildThreadPoolExecutor,而不是使用返回ExecutorService对象的Executors工厂。 你总是可以创build自己的返回ThreadPoolExecutors的工厂,但是仍然会留下使用具体types的错误forms,而不是它的接口。
一种可能性是提供自己的ThreadFactory,它在已知的线程组中创build线程,然后您可以进行计数
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; 公共类PoolSize2 { public static void main(String [] args){ final ThreadGroup threadGroup = new ThreadGroup(“workers”); ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory(){ 公共线程newThread(Runnable r){ 返回新的线程(threadGroup,r); } }); 的System.out.println(threadGroup.activeCount()); } }
我有同样的问题,所以创build了一个简单的Runnable来跟踪一个ExecutorService实例。
import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; public class ExecutorServiceAnalyzer implements Runnable { private final ThreadPoolExecutor threadPoolExecutor; private final int timeDiff; public ExecutorServiceAnalyzer(ExecutorService executorService, int timeDiff) { this.timeDiff = timeDiff; if (executorService instanceof ThreadPoolExecutor) { threadPoolExecutor = (ThreadPoolExecutor) executorService; } else { threadPoolExecutor = null; System.out.println("This executor doesn't support ThreadPoolExecutor "); } } @Override public void run() { if (threadPoolExecutor != null) { do { System.out.println("#### Thread Report:: Active:" + threadPoolExecutor.getActiveCount() + " Pool: " + threadPoolExecutor.getPoolSize() + " MaxPool: " + threadPoolExecutor.getMaximumPoolSize() + " ####"); try { Thread.sleep(timeDiff); } catch (Exception e) { } } while (threadPoolExecutor.getActiveCount() > 1); System.out.println("##### Terminating as only 1 thread is active ######"); } } }
你可以简单地用你的执行器来获取ThreadPool的状态
防爆
ExecutorService executorService = Executors.newFixedThreadPool(4); executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));
在线程中放置一个静态易失性计数器,每当线程被激活和closures时都会更新。 另请参阅API。