OnJava8-Examples/threads/PriorityBlockingQueueDemo.java

157 lines
4.2 KiB
Java
Raw Normal View History

2016-07-05 14:46:09 -06:00
// threads/PriorityBlockingQueueDemo.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
import java.util.concurrent.*;
import java.util.*;
class PrioritizedTask implements
Runnable, Comparable<PrioritizedTask> {
2016-01-25 18:05:55 -08:00
private SplittableRandom rand = new SplittableRandom(47);
2015-06-15 17:47:35 -07:00
private static int counter = 0;
private final int id = counter++;
private final int priority;
protected static List<PrioritizedTask> sequence =
new ArrayList<>();
public PrioritizedTask(int priority) {
this.priority = priority;
sequence.add(this);
}
@Override
public int compareTo(PrioritizedTask arg) {
return priority < arg.priority ? 1 :
(priority > arg.priority ? -1 : 0);
}
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(250));
} catch(InterruptedException e) {
// Acceptable way to exit
}
2015-11-03 12:00:44 -08:00
System.out.println(this);
2015-06-15 17:47:35 -07:00
}
@Override
public String toString() {
return String.format("[%1$-3d]", priority) +
" Task " + id;
}
public String summary() {
return "(" + id + ":" + priority + ")";
}
2016-01-25 18:05:55 -08:00
public static
class EndSentinel extends PrioritizedTask {
2015-06-15 17:47:35 -07:00
private ExecutorService exec;
public EndSentinel(ExecutorService e) {
super(-1); // Lowest priority in this program
exec = e;
}
@Override
public void run() {
int count = 0;
for(PrioritizedTask pt : sequence) {
2015-11-03 12:00:44 -08:00
System.out.print(pt.summary());
2015-06-15 17:47:35 -07:00
if(++count % 5 == 0)
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
}
2015-11-03 12:00:44 -08:00
System.out.println();
System.out.println(this + " Calling shutdownNow()");
2015-06-15 17:47:35 -07:00
exec.shutdownNow();
}
}
}
class PrioritizedTaskProducer implements Runnable {
2016-01-25 18:05:55 -08:00
private SplittableRandom rand = new SplittableRandom(47);
2015-06-15 17:47:35 -07:00
private Queue<Runnable> queue;
private ExecutorService exec;
public PrioritizedTaskProducer(
Queue<Runnable> q, ExecutorService e) {
queue = q;
exec = e; // Used for EndSentinel
}
@Override
public void run() {
// Unbounded queue; never blocks.
// Fill it up fast with random priorities:
for(int i = 0; i < 20; i++) {
queue.add(new PrioritizedTask(rand.nextInt(10)));
Thread.yield();
}
// Trickle in highest-priority jobs:
try {
for(int i = 0; i < 10; i++) {
TimeUnit.MILLISECONDS.sleep(250);
queue.add(new PrioritizedTask(10));
}
// Add jobs, lowest priority first:
for(int i = 0; i < 10; i++)
queue.add(new PrioritizedTask(i));
// A sentinel to stop all the tasks:
queue.add(new PrioritizedTask.EndSentinel(exec));
} catch(InterruptedException e) {
// Acceptable way to exit
}
2016-01-25 18:05:55 -08:00
System.out.println(
"Finished PrioritizedTaskProducer");
2015-06-15 17:47:35 -07:00
}
}
class PrioritizedTaskConsumer implements Runnable {
private PriorityBlockingQueue<Runnable> q;
public PrioritizedTaskConsumer(
PriorityBlockingQueue<Runnable> q) {
this.q = q;
}
@Override
public void run() {
try {
while(!Thread.interrupted())
// Use current thread to run the task:
q.take().run();
} catch(InterruptedException e) {
// Acceptable way to exit
}
2016-01-25 18:05:55 -08:00
System.out.println(
"Finished PrioritizedTaskConsumer");
2015-06-15 17:47:35 -07:00
}
}
public class PriorityBlockingQueueDemo {
2016-01-25 18:05:55 -08:00
public static void
main(String[] args) throws Exception {
ExecutorService es = Executors.newCachedThreadPool();
2015-06-15 17:47:35 -07:00
PriorityBlockingQueue<Runnable> queue =
new PriorityBlockingQueue<>();
2016-01-25 18:05:55 -08:00
es.execute(new PrioritizedTaskProducer(queue, es));
es.execute(new PrioritizedTaskConsumer(queue));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
2016-07-20 06:32:39 -06:00
/* Output: (First and Last 12 Lines)
2015-06-15 17:47:35 -07:00
[9 ] Task 5
2016-07-22 14:45:35 -06:00
[8 ] Task 7
[10 ] Task 20
[8 ] Task 8
[10 ] Task 21
2016-07-27 11:12:11 -06:00
[7 ] Task 4
2016-07-22 14:45:35 -06:00
[10 ] Task 22
2016-07-27 11:12:11 -06:00
[7 ] Task 19
2016-07-22 14:45:35 -06:00
[10 ] Task 23
2016-07-27 11:12:11 -06:00
[7 ] Task 11
2016-07-22 14:45:35 -06:00
[10 ] Task 24
[7 ] Task 1
...________...________...________...________...
[0 ] Task 16
(0:5)(1:7)(2:1)(3:0)(4:7)
(5:9)(6:6)(7:8)(8:8)(9:3)
(10:0)(11:7)(12:0)(13:5)(14:2)
(15:4)(16:0)(17:2)(18:1)(19:7)
2015-06-15 17:47:35 -07:00
(20:10)(21:10)(22:10)(23:10)(24:10)
(25:10)(26:10)(27:10)(28:10)(29:10)
(30:0)(31:1)(32:2)(33:3)(34:4)
(35:5)(36:6)(37:7)(38:8)(39:9)
(40:-1)
[-1 ] Task 40 Calling shutdownNow()
Finished PrioritizedTaskConsumer
2015-09-07 11:44:36 -06:00
*/