开发者

Executors.newSingleThreadExecutor() - how to see how many tasks are in queue

I am using Executors.newSingleThreadExecutor() in my code. I want to monitor number of tasks in queue to check that procesor is not overloaded with messages. How can I get a number of not c开发者_如何学JAVAompleted submited tasks for curent moment? I expect something like this:

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
executor.submit(new Runnable() {...});
// do something and get 3
...
// do something and get 2
...
// do something and get 1

Thanks


Just in case you want to guarantee that even with new versions of the JVM the code suggested by FlorianOver will work, you could do it this way: (For the case that the method Executors.newSingleThreadExecutor() will no longer return an instance of type ThreadPoolExecutor)

ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

// ...

int queueSize = executor.getQueue().size();


Isn't it like this?

((ThreadPoolExecutor) executor).getQueue().size();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜