Java Executors:如何设置任务优先级? [英] Java Executors: how can I set task priority?
问题描述
是否有可能为执行者执行的任务设置优先级?我在JCIP中发现了一些关于它的可能,但我找不到任何例子,我找不到任何相关的文档。
Is there a possibility to set priority to tasks which are executed by Executors? I've found some statements in JCIP about it's possible but I cannot find any example and I cannot find anything related in docs.
从JCIP:
执行策略指定
执行任务
的什么,在哪里,何时以及如何执行,包括:
An execution policy specifies the "what, where, when, and how" of task execution, including:
- ...
- 执行任务的顺序应为FIFO,LIFO, strong>)?
- ...
UPD :我意识到我不是问我想问什么。我真正想要的是:
UPD: I realized that I asked not exactly what I wanted to ask. What I really wanted is:
如何使用/模拟设置线程优先级(即 thread.setPriority()
)与executors框架?
How to use/emulate setting threads priority (i.e. what was thread.setPriority()
) with executors framework?
推荐答案
目前只有执行程序界面是 ThreadPoolExecutor 和 ScheduledThreadpoolExecutor
而不是使用实用程序/工厂类 Executors ,您应该使用构造函数创建一个实例。
Instead of using the utility / factory class Executors, you should create an instance using a constructor.
You can pass a BlockingQueue to the constructors of the ThreadPoolExecutor.
BlockingQueue的一个实现,rel =nofollow noreferrer> BlockingQueue 到ThreadPoolExecutor的构造函数。 ://java.sun.com/javase/6/docs/api/java/util/concurrent/PriorityBlockingQueue.htmlrel =nofollow noreferrer> PriorityBlockingQueue 可让您将Comparator传递到构造函数,即方式让您决定执行的顺序。
One of the implementations of the BlockingQueue, the PriorityBlockingQueue lets you pass a Comparator to a constructor, that way enabling you to decide the order of execution.
这篇关于Java Executors:如何设置任务优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!