在运行多任务时,有多种选择策略,其中一种是优先级高优先,java可以通过PriorityBlockingQueue来实现。
代码例子:
//优先运行任务
class PrioritizedTask implements
Runnable, Comparable<PrioritizedTask> {
private Random rand = new Random(47);
private static int counter = 0;
private final int id = counter++;
private final int priority;
protected static List<PrioritizedTask> sequence =
new ArrayList<PrioritizedTask>();
public PrioritizedTask(int priority) {
this.priority = priority;
sequence.add(this);
}
//优先级比较
public int compareTo(PrioritizedTask arg) {
return priority < arg.priority ? 1 :
(priority > arg.priority ? -1 : 0);
}
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(250));
} catch(InterruptedException e) {
// Acceptable way to exit
}
System.out.println(this);
}
public String toString() {
return String.format("[%1$-3d]", priority) +
" Task " + id;
}
public String summary() {
return "(" + id + ":" + priority + ")";
}
//终止任务的终止类
public static class EndSentinel extends PrioritizedTask {
private ExecutorService exec;
public EndSentinel(ExecutorService e) {
super(-1); // Lowest priority in this program
exec = e;
}
public void run() {
int count = 0;
System.out.println(this + " Calling shutdownNow()");
exec.shutdownNow();
}
}
}
//优先队列存储
class PrioritizedTaskProducer implements Runnable {
private Random rand = new Random(47);
private Queue<Runnable> queue;
private ExecutorService exec;
public PrioritizedTaskProducer(
Queue<Runnable> q, ExecutorService e) {
queue = q;
exec = e;
}
public void run() {
for(int i = 0; i < 20; i++) {
queue.add(new PrioritizedTask(rand.nextInt(10)));
Thread.yield();
}
try {
for(int i = 0; i < 10; i++) {
TimeUnit.MILLISECONDS.sleep(250);
queue.add(new PrioritizedTask(10));
}
for(int i = 0; i < 10; i++)
queue.add(new PrioritizedTask(i));
// 终止所有任务
queue.add(new PrioritizedTask.EndSentinel(exec));
} catch(InterruptedException e) {
}
System.out.println("Finished PrioritizedTaskProducer");
}
}
//优先任务测试
class PrioritizedTaskConsumer implements Runnable {
private PriorityBlockingQueue<Runnable> q;
public PrioritizedTaskConsumer(
PriorityBlockingQueue<Runnable> q) {
this.q = q;
}
public void run() {
try {
while(!Thread.interrupted())
// 用这个线程执行任务
q.take().run();
} catch(InterruptedException e) {
}
System.out.println("Finished PrioritizedTaskConsumer");
}
}
public class PriorityBlockingQueueDemo {
public static void main(String[] args) throws Exception {
Random rand = new Random(47);
ExecutorService exec = Executors.newCachedThreadPool();
PriorityBlockingQueue<Runnable> queue =
new PriorityBlockingQueue<Runnable>();
exec.execute(new PrioritizedTaskProducer(queue, exec));
exec.execute(new PrioritizedTaskConsumer(queue));
}
}
运行结果:
[8 ] Task 0
[9 ] Task 5
[9 ] Task 13
[9 ] Task 14
[8 ] Task 10
[8 ] Task 16
[8 ] Task 19
[8 ] Task 11
[8 ] Task 6
[8 ] Task 15
[7 ] Task 9
[5 ] Task 1
[3 ] Task 2
[2 ] Task 8
[1 ] Task 3
[1 ] Task 4
[1 ] Task 17
[1 ] Task 12
[0 ] Task 18
[0 ] Task 7
[10 ] Task 20
[10 ] Task 21
[10 ] Task 22
[10 ] Task 23
[10 ] Task 24
[10 ] Task 25
[10 ] Task 26
[10 ] Task 27
[10 ] Task 28
Finished PrioritizedTaskProducer
[10 ] Task 29
[9 ] Task 39
[8 ] Task 38
[7 ] Task 37
[6 ] Task 36
[5 ] Task 35
[4 ] Task 34
[3 ] Task 33
[2 ] Task 32
[1 ] Task 31
[0 ] Task 30
[-1 ] Task 40 Calling shutdownNow()
Finished PrioritizedTaskConsumer