所以我使用executorservice来创建一个线程池。
ExecutorService executor = Executors.newSingleThreadExecutor();
我正在尝试访问线程池队列中的任务数。我发现没有任何方法可以实现这一点。我知道有一些方法可以在threadpoolexecutor中获取队列大小,但是我如何使用executorservice对象来实现这一点呢?
就像我说的,如果我像这样创建了一个ThreadpoolExecutor,我就可以获得队列信息
ThreadPoolExecutor tpExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
我知道我可以使用tpExecutor.queue.size()来获取线程池队列中的任务数。但目前我已经使用Executor Service声明了我的线程池。我该怎么做呢?如果人们可以编写代码并进行演示,那将是值得欣赏的。
发布于 2019-03-29 08:29:41
我认为这应该行得通:
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) this.executorService;
int activeCount = threadPoolExecutor.getActiveCount();
long taskCount = threadPoolExecutor.getTaskCount();
long completedTaskCount = threadPoolExecutor.getCompletedTaskCount();
long tasksToDo = taskCount - completedTaskCount - activeCount;
发布于 2017-09-27 01:32:48
您可以将此转换为ThreadPoolExecutor。
ThreadPoolExecutor ex=(ThreadPoolExecutor)executor;ex.getQueue().size();
https://stackoverflow.com/questions/46018534
复制相似问题