4

I am using this code to get an ExecutorService in java:

this.exec = Executors.newSingleThreadExecutor();

Now I would like to know how many jobs it has processed, how many are in the queue (most importantly!), and basically all the information I could get. But an ordinary ExecutorService does not have this information. But according to the answer in this question, I could typecast it to ThreadPoolExecutor because that's what it returns. Well, that sounds easy... but there's one problem. My machine does not return ThreadPoolExecutor!

Instead I am given a java.util.concurrent.Executors$FinalizableDelegatedExecutorService which, when trying to typecast to that, is impossible because the class is not visible.

So how can I find out the status of my ExecutorService?

If you are very curious and would like to know what I am using my ExecutorService for (if that matters), read this question that I have asked earlier. It's basically for SQL statements.

Community
  • 1
  • 1
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108

1 Answers1

4

Option 1

You can create your own ThreadPoolExecutor - this is what newFixedThreadPool does:

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

You can then keep a reference to the queue to see how full it is. You can even pass in a modified version which keeps a counter of number of jobs received.

Option 2

You could use an ExecutorCompletionService which will help you know when a task completes - you can then count how many jobs you have submitted / how many have completed.

assylias
  • 321,522
  • 82
  • 660
  • 783