Java Executor,在队列底部/末尾添加新任务
我希望有一个执行器可以选择是否将新任务添加到队列的顶部或底部。实现这一目标的最简单方法是什么?
I want to have an Executor with the possibility to choose whether new task is added at the top or at the bottom of the queue. What is the simplest way to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以通过使用
PriorityQeueue
(或PriorityBlockingQueue
根据您的需要)来实现此功能。这些队列按照比较器的顺序处理任务。尽管此比较器在创建时会传递到队列,但您可以实现自定义比较器,该比较器具有自己的逻辑,可以决定特定任务的优先级是较低还是较高。
I think that you can achieve this functionality by using
PriorityQeueue
(orPriorityBlockingQueue
depending on your needs).These queues process tasks in order that depends on Comparator. Although this comparator is passed to the queue when it is created you can implement your custom comparator that has its own logic that can decide whether specific task is lower or higher priority.