views:

60

answers:

3

I load a lot of images form internet with a ThreadPoolExecutor.

When new images found, I need to render it first, in that case I want to abandon the old tasks which are still queued in the ThreadPoolExecutor and added these new items to download.

I found there are no "clear queue" method in ThreadPoolExecutor, and "purge" method sounds like not good for this.

What should I do?

I just thought to call "shutdown" of this executor and recreate a new one to do this, not sure whether it is appropriate.

+1  A: 

ThreadPoolExecutor has a remove() method. You can use that in conjunction with getQueue() or keep your own collection of Runnables that have been queue and might need removal. If you keep your own collection, remember to have some mechinism that removes the runnable from your collection when it has finished.

unholysampler
+2  A: 

Have you tried this?

ThreadPoolExecutor pool = .....;  pool.remove(task);

task is the Runnable you want to remove.

or if you want to clear the queue.

pool.getQueue().clear() 
punkers
Although the javadoc says you really shouldn't use the getQueue method, that is probably the approach you want to go with based on the OP's question.
Tim Bender
Yes, the javadoc says the getQueue method is not suggest to use, I just thought that is not a good way.
virsir
A: 

I doubt that getQueue().clear() can work for this, the doc just says

Method ThreadPoolExecutor#getQueue allows access to the work queue for purposes of monitoring and debugging. Use of this method for any other purpose is strongly discouraged

bob