I'm receiving a sequence of messages, and I want to process them in their sequential order. Each message has a sequence number. There's a pool of threads receiving them. I want to put them into a blocking queue like a PriorityBlockingQueue, and read them in the right order, blocking until the next consecutive message is available.
E.g. given this code:
ConsecutiveBlockingQueue<Integer> q = new ConsecutiveBlockingQueue<>();
new Thread (()->{ q.put(0); q.put(2); }).start();
new Thread (()->{ q.put(1); q.put(3); }).start();
ArrayList<Integer> ordered = new ArrayList<>(4);
for (int i = 0; i < 4; i++) {
ordered.add(q.take());
}
System.out.println(ordered);
I want it to print [0, 1, 2, 3]
PriorityBlockingQueuedoes. Your question?PriorityQueueinternally.