Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep track of tasks submitted to ThreadPoolExecutor

I am running several tasks in a ThreadPoolExecutor. I initialise it as follows:

private VideoExportExecutor executor;
private BlockingQueue<Runnable> jobQueue;

public void initialiseVideoProcessor()
{
    jobQueue = new LinkedBlockingQueue<Runnable>();
    executor = new VideoExportExecutor(1, 1, Long.MAX_VALUE, TimeUnit.SECONDS, jobQueue);
}

I have made my own implementation of runnable (VideoExportThread) which contains a getProgress() method to keep track of the progress of submitted tasks. I submit instances of this as follows:

executor.submit(new VideoExportThread(gcmPath));

I am after a way to query the executor/blockingQueue for current/pending threads. I have tried to use jobQueue.toArray() and to override the executor method beforeExecute(Thread t, Runnable r) but in both cases the returned runnable is of type FutureTask which does not contain much data. Is there a way for me to use it to retrieve my original VideoExportThread instance in order to identify which ones are running and to query its progress?

Thanks

like image 482
tishu Avatar asked Mar 11 '13 14:03

tishu


1 Answers

Why not simply keep a list of your Runnables?

List<Runnable> runnables = new ArrayList<> ();
VideoExportThread r = new VideoExportThread(gcmPath);
runnables.add(r);
executor.submit(r);

Also note that executor.submit(r); returns a Future - you can call its isDone() method to check if the submitted task is still running.


Side comment: there might be a good reason to manage the job queue manually, but if not, you use one of the factory methods to make your life easier. For example: ExecutorService executor = Executors.newCachedThreadPool();.

like image 145
assylias Avatar answered Nov 12 '22 19:11

assylias