0

I have a ThreadPoolExecutor with a fixed core pool size and a LinkedBlockingQueue for thread pool tasks that have to wait if the entire core is busy.

Assuming that all core threads are currently busy and there are further tasks enqueued into the queue that are waiting for their future execution.

I'd like to detect the queue position of a specific task which is currently waiting. It's possible to access all queue items via ThreadPoolExecutor.getQueue().toArray() but the items are all instances of FutureTask. I cannot access the contained Callable member which would provide enough information for an identification of my concrete task which I'm looking for.

Is there an existing mechanism or approach to detect the position of a wanted task which is currently queued?

PAX
  • 1,056
  • 15
  • 33
  • 1
    I don't know about any existing mechanism, but there's an approach that's simple enough: Count them. Label each task with a serial number from counter A when you submit it, and have each task increment counter B as soon as it starts to run. Trivial math will tell you how many tasks are in the queue and, the position of a task with label N. – Solomon Slow Jan 14 '16 at 18:09
  • That's genius! Thanks @jameslarge ! Please add this hint as answer. In the end, if there's no official solution for my requirement then I'm going to accept your answer. – PAX Jan 14 '16 at 18:18

1 Answers1

0

I don't know about any existing mechanism, but there's an approach that's simple enough: Count them.

Label each task with a serial number from counter A when you submit it, and have each task increment counter B as soon as it starts to run. Trivial math will tell you how many tasks are in the queue and, the position of a task with label N

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57