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();
|
2017-01-22 16:48:11 -08:00
|
|
|
private final int id = counter.getAndIncrement();
|
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<>();
|
2017-05-01 14:33:10 -06:00
|
|
|
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 {
|
2017-05-01 14:33:10 -06:00
|
|
|
EndSentinel() { super(-1); }
|
2017-01-13 07:03:28 -08:00
|
|
|
}
|
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;
|
2017-05-01 14:33:10 -06:00
|
|
|
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)
|
2017-01-22 16:48:11 -08:00
|
|
|
.peek(p -> new Nap(rand.nextDouble() / 10))
|
2017-01-13 07:03:28 -08:00
|
|
|
.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);
|
|
|
|
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;
|
|
|
|
}
|
2017-01-22 16:48:11 -08:00
|
|
|
new Nap(rand.nextDouble() / 10);
|
2017-01-13 07:03:28 -08:00
|
|
|
} 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
|
|
|
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:
|
2017-01-22 16:48:11 -08:00
|
|
|
[15] Prioritized 1
|
|
|
|
[17] Prioritized 0
|
|
|
|
[17] Prioritized 5
|
|
|
|
[16] Prioritized 6
|
|
|
|
[14] Prioritized 9
|
|
|
|
[12] Prioritized 2
|
2017-01-13 07:03:28 -08:00
|
|
|
[11] Prioritized 4
|
2017-01-22 16:48:11 -08:00
|
|
|
[11] Prioritized 12
|
|
|
|
[13] Prioritized 13
|
|
|
|
[12] Prioritized 16
|
|
|
|
[14] Prioritized 18
|
|
|
|
[15] Prioritized 23
|
|
|
|
[18] Prioritized 26
|
|
|
|
[16] Prioritized 29
|
|
|
|
[12] Prioritized 17
|
|
|
|
[11] Prioritized 30
|
|
|
|
[11] Prioritized 24
|
|
|
|
[10] Prioritized 15
|
|
|
|
[10] Prioritized 22
|
|
|
|
[8] Prioritized 25
|
|
|
|
[8] Prioritized 11
|
|
|
|
[8] Prioritized 10
|
|
|
|
[6] Prioritized 31
|
|
|
|
[3] Prioritized 7
|
|
|
|
[2] Prioritized 20
|
|
|
|
[1] Prioritized 3
|
|
|
|
[0] Prioritized 19
|
|
|
|
[0] Prioritized 8
|
|
|
|
[0] Prioritized 14
|
2017-01-13 07:03:28 -08:00
|
|
|
[0] Prioritized 21
|
2017-01-22 16:48:11 -08:00
|
|
|
[-1] Prioritized 28
|
|
|
|
(0:17)(2:12)(1:15)(3:1)(4:11)
|
|
|
|
(5:17)(6:16)(7:3)(8:0)(9:14)
|
|
|
|
(10:8)(11:8)(12:11)(13:13)(14:0)
|
|
|
|
(15:10)(16:12)(17:12)(18:14)(19:0)
|
|
|
|
(20:2)(21:0)(22:10)(23:15)(24:11)
|
|
|
|
(25:8)(26:18)(27:-1)(28:-1)(29:16)
|
|
|
|
(30:11)(31:6)(32:-1)
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|