开发者

How to remove old queued tasks in ThreadPoolExecutor and insert new tasks instead?

开发者 https://www.devze.com 2023-01-20 23:31 出处:网络
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 ThreadPool

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开发者_StackOverflow中文版 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.


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() 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号