简体   繁体   English

在队列中查找已生成的任务的策略

[英]Strategy for finding already spawned task in queue

I am writing a module where a number of threads will be spawned which performs some task. 我正在编写一个模块,其中将产生许多执行某些任务的线程。 So overall I have a 'Manager' class which provides information to a 'Resolver' class which create and submits a 'Task' using threadpool. 所以总的来说,我有一个'Manager'类,它向'Resolver'类提供信息,该类使用线程池创建并提交'Task'。

Resolver has a threadpool executor which has unbounded queue that I am using to check if perticular task is already spawned. 解析器具有一个线程池执行器,该执行器具有无限制的队列,我正在使用该队列来检查是否已生成了特定任务。 If there is already an instance of the task in the queue, I remove it using ThreadPoolExecutor and submit a new task again. 如果队列中已经有该任务的实例,则可以使用ThreadPoolExecutor将其删除,然后再次提交新任务。 This is done intentionally since new task brings new info from the 'Manager'. 这是有意完成的,因为新任务从“经理”那里带来了新的信息。

I am using ThreadPoolexecutor to find if the task is already is queue. 我正在使用ThreadPoolexecutor查找任务是否已经在队列中。 But I am worried about the performance of the queue and feel that hash map would be a better choice since queue might have more than 30000 instances of task. 但是我担心队列的性能,并认为哈希映射将是一个更好的选择,因为队列可能有超过30000个任务实例。

Guide me to choose an appropriate strategy for finding already spawned task in queue. 指导我选择合适的策略来查找队列中已生成的任务。

I don't think it would cause any problems (unless you implement too complicated equals implementation). 我认为这不会造成任何问题(除非您实现了过于复杂的equals实现)。 I would recommend to run a test with profiler and take a look if it is a real problem or not... 我建议使用Profiler进行测试,看看是否存在真正的问题...

However if it is, you might create your own implementation of BlockingQueue (it might be passed into ThreadPollExecutor as a contructor argument). 但是,如果是这样,则可以创建自己的BlockingQueue实现(它可以作为构造函数参数传递到ThreadPollExecutor中)。 This way you might create some sort of hybrid some implementation of BlockingQueue (for standard work queue) and HashSet (for fast contains(...) test). 这样,您可能会创建某种混合的BlockingQueue(对于标准工作队列)和HashSet(对于快速contains(...)测试)的某些实现。 Remember about proper synchronization for all operations on those collections. 记住有关这些集合上所有操作的正确同步。 All operations must be atomic. 所有操作必须是原子的。

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

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