OnJava8-Examples/lowlevel/PriorityBlockingQueueDemo.java

144 lines
3.7 KiB
Java
Raw Normal View History

2016-12-07 10:34:41 -08:00
// lowlevel/PriorityBlockingQueueDemo.java
2016-12-30 17:23:13 -08:00
// (c)2017 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.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import java.util.*;
2017-01-13 07:03:28 -08:00
import java.util.stream.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
2016-12-21 11:06:49 -08:00
import onjava.Nap;
2015-06-15 17:47:35 -07:00
2017-01-13 07:03:28 -08:00
class Prioritized implements Comparable<Prioritized> {
private static AtomicInteger counter =
new AtomicInteger();
private final int id = counter.getAndAdd(1);
2015-06-15 17:47:35 -07:00
private final int priority;
2017-01-13 07:03:28 -08:00
private static List<Prioritized> sequence =
new CopyOnWriteArrayList<>();
public Prioritized(int priority) {
2015-06-15 17:47:35 -07:00
this.priority = priority;
sequence.add(this);
}
@Override
2017-01-13 07:03:28 -08:00
public int compareTo(Prioritized arg) {
2015-06-15 17:47:35 -07:00
return priority < arg.priority ? 1 :
(priority > arg.priority ? -1 : 0);
}
@Override
public String toString() {
2017-01-13 07:03:28 -08:00
return String.format(
"[%d] Prioritized %d", priority, id);
2015-06-15 17:47:35 -07:00
}
2017-01-13 07:03:28 -08:00
public void displaySequence() {
int count = 0;
for(Prioritized pt : sequence) {
System.out.printf("(%d:%d)", pt.id, pt.priority);
if(++count % 5 == 0)
System.out.println();
2015-06-15 17:47:35 -07:00
}
}
2017-01-13 07:03:28 -08:00
public static class EndSentinel extends Prioritized {
public EndSentinel() { super(-1); }
}
2015-06-15 17:47:35 -07:00
}
2017-01-13 07:03:28 -08:00
class Producer implements Runnable {
private static AtomicInteger seed =
new AtomicInteger(47);
private SplittableRandom rand =
new SplittableRandom(seed.getAndAdd(10));
private Queue<Prioritized> queue;
public Producer(Queue<Prioritized> q) {
2015-06-15 17:47:35 -07:00
queue = q;
}
@Override
public void run() {
2017-01-13 07:03:28 -08:00
rand.ints(10, 0, 20)
.mapToObj(Prioritized::new)
.peek(p -> new Nap(rand.nextInt(
PriorityBlockingQueueDemo.NAPTIME)))
.forEach(p -> queue.add(p));
queue.add(new Prioritized.EndSentinel());
2015-06-15 17:47:35 -07:00
}
}
2017-01-13 07:03:28 -08:00
class Consumer implements Runnable {
private PriorityBlockingQueue<Prioritized> q;
private SplittableRandom rand =
new SplittableRandom(47);
public
Consumer(PriorityBlockingQueue<Prioritized> q) {
2015-06-15 17:47:35 -07:00
this.q = q;
}
@Override
public void run() {
2017-01-13 07:03:28 -08:00
while(true) {
try {
Prioritized pt = q.take();
System.out.println(pt);
if(pt instanceof Prioritized.EndSentinel) {
pt.displaySequence();
break;
}
new Nap(rand.nextInt(
PriorityBlockingQueueDemo.NAPTIME));
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
2015-06-15 17:47:35 -07:00
}
}
}
public class PriorityBlockingQueueDemo {
2017-01-13 07:03:28 -08:00
static final int NAPTIME = 50;
public static void main(String[] args) {
PriorityBlockingQueue<Prioritized> queue =
2015-06-15 17:47:35 -07:00
new PriorityBlockingQueue<>();
2017-01-13 07:03:28 -08:00
CompletableFuture.runAsync(new Producer(queue));
CompletableFuture.runAsync(new Producer(queue));
CompletableFuture.runAsync(new Producer(queue));
CompletableFuture.runAsync(new Consumer(queue))
.join();
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
2017-01-13 07:03:28 -08:00
/* Output:
[12] Prioritized 1
[17] Prioritized 2
[15] Prioritized 0
[18] Prioritized 17
[17] Prioritized 10
[16] Prioritized 20
[16] Prioritized 16
[15] Prioritized 15
[14] Prioritized 8
[14] Prioritized 13
[13] Prioritized 12
[12] Prioritized 19
[12] Prioritized 14
[11] Prioritized 6
[11] Prioritized 22
[11] Prioritized 4
[11] Prioritized 31
[10] Prioritized 30
[10] Prioritized 26
[8] Prioritized 18
[8] Prioritized 23
[8] Prioritized 9
[6] Prioritized 24
[3] Prioritized 3
[2] Prioritized 29
[1] Prioritized 7
[0] Prioritized 27
[0] Prioritized 5
[0] Prioritized 21
[0] Prioritized 11
[-1] Prioritized 25
(0:15)(2:17)(1:12)(3:3)(4:11)
(5:0)(6:11)(7:1)(8:14)(9:8)
(10:17)(11:0)(12:13)(13:14)(14:12)
(15:15)(16:16)(17:18)(18:8)(19:12)
(20:16)(21:0)(22:11)(23:8)(24:6)
(25:-1)(26:10)(27:0)(28:-1)(29:2)
(30:10)(31:11)(32:-1)
2015-09-07 11:44:36 -06:00
*/