2015-09-07 11:44:36 -06:00
|
|
|
|
// containersindepth/QueueBehavior.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Compares the behavior of some of the queues
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
import java.util.*;
|
2015-11-03 12:00:44 -08:00
|
|
|
|
import java.util.function.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
|
import onjava.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
|
|
public class QueueBehavior {
|
|
|
|
|
private static int count = 10;
|
2015-11-03 12:00:44 -08:00
|
|
|
|
static <T> void test(Queue<T> queue, Supplier<T> gen) {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
for(int i = 0; i < count; i++)
|
2015-11-03 12:00:44 -08:00
|
|
|
|
queue.offer(gen.get());
|
2015-06-15 17:47:35 -07:00
|
|
|
|
while(queue.peek() != null)
|
|
|
|
|
System.out.print(queue.remove() + " ");
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
2015-11-03 12:00:44 -08:00
|
|
|
|
static class Gen implements Supplier<String> {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
String[] s = ("one two three four five six seven " +
|
|
|
|
|
"eight nine ten").split(" ");
|
|
|
|
|
int i;
|
|
|
|
|
@Override
|
2015-11-03 12:00:44 -08:00
|
|
|
|
public String get() { return s[i++]; }
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
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
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|