how to traverse a queue in java

I have a ThreadPoolExecutor that fulfils my DownloadTask implements Runnable{private String taskId... tasks and I need to have possibility in any time to get a define task from the ThreadPoolExecutor queue, for example, using a task id:

mDownloadTasksPool.getQueue();

what is the best way to travesre a queue and to get a define element?

It’s possible to have a list with all duplicates tasks but it looks not very elegant.

Answer

Subclass your Runnables to provide a getter for taskID (if that’s your key). Create a concurrent map, mapping your taskID to the Runnable that you put in the queue. Subclass your ThreadPoolExecutor. In beforeExecute, you remove the current task from that map since it’s not in the queue anymore. You may want to cast the Runnable to your custom class to use that taskID getter.

Leave a Reply

Your email address will not be published. Required fields are marked *