// containersindepth/QueueBehavior.java // ©2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Compares the behavior of some of the queues import java.util.concurrent.*; import java.util.*; import java.util.function.*; import onjava.*; public class QueueBehavior { private static int count = 10; static void test(Queue queue, Supplier gen) { for(int i = 0; i < count; i++) queue.offer(gen.get()); while(queue.peek() != null) System.out.print(queue.remove() + " "); System.out.println(); } static class Gen implements Supplier { String[] s = ("one two three four five six seven " + "eight nine ten").split(" "); int i; @Override public String get() { return s[i++]; } } public static void main(String[] args) { test(new LinkedList<>(), new Gen()); test(new PriorityQueue<>(), new Gen()); test(new ArrayBlockingQueue<>(count), new Gen()); test(new ConcurrentLinkedQueue<>(), new Gen()); test(new LinkedBlockingQueue<>(), new Gen()); test(new PriorityBlockingQueue<>(), new Gen()); } } /* Output: one two three four five six seven eight nine ten eight five four nine one seven six ten three two one two three four five six seven eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten eight five four nine one seven six ten three two */