简体   繁体   English

如何在ThreadPoolExecutor中删除旧的排队任务并插入新任务?

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

I load a lot of images form internet with a ThreadPoolExecutor. 我使用ThreadPoolExecutor从Internet加载了大量图像。

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. 找到新图像后,我需要先渲染它,在这种情况下,我想放弃仍在ThreadPoolExecutor中排队的旧任务,并添加这些新项目进行下载。

I found there are no "clear queue" method in ThreadPoolExecutor, and "purge" method sounds like not good for this. 我发现ThreadPoolExecutor中没有“清除队列”方法,而“清除”方法听起来对此并不好。

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. task是您要删除的Runnable。

or if you want to clear the queue. 或者如果要清除队列。

pool.getQueue().clear() 

ThreadPoolExecutor has a remove() method. ThreadPoolExecutor有一个remove()方法。 You can use that in conjunction with getQueue() or keep your own collection of Runnable s that have been queue and might need removal. 您可以将它与getQueue()结合使用,或者保留自己的已经队列且可能需要删除的Runnable集合。 If you keep your own collection, remember to have some mechinism that removes the runnable from your collection when it has finished. 如果你保留自己的收藏品,请记住要有一些机器人在收集完成后从你的收藏品中删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM