OnJava8-Examples/containers/QueueBehavior.java

40 lines
1.3 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: containers/QueueBehavior.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Compares the behavior of some of the queues
import java.util.concurrent.*;
import java.util.*;
import net.mindview.util.*;
public class QueueBehavior {
private static int count = 10;
static <T> void test(Queue<T> queue, Generator<T> gen) {
for(int i = 0; i < count; i++)
queue.offer(gen.next());
while(queue.peek() != null)
System.out.print(queue.remove() + " ");
System.out.println();
}
static class Gen implements Generator<String> {
String[] s = ("one two three four five six seven " +
"eight nine ten").split(" ");
int i;
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String next() { return s[i++]; }
}
public static void main(String[] args) {
2015-05-05 11:20:13 -07:00
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-04-20 15:36:01 -07:00
}
} /* 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
*///:~