-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
59 lines (47 loc) · 1.64 KB
/
Queue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
import java.util.concurrent.*;
/**
* @author viktorvoltz
*/
interface Generator<T> {
T next();
}
class Genx implements Generator<String> {
String[] d = ("home away hello there lot nolax prod " + "twenty teo po op ex").split(" ");
int i;
public String next() {
return d[i++];
}
}
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;
public String next() {
return s[i++];
}
}
public static void main(String[] args) {
test(new LinkedList<String>(), new Gen());
test(new LinkedList<String>(), new Genx());
test(new PriorityQueue<String>(), new Gen());
test(new PriorityQueue<String>(), new Genx());
test(new ArrayBlockingQueue<String>(count), new Gen());
test(new ArrayBlockingQueue<String>(count), new Genx());
test(new ConcurrentLinkedQueue<String>(), new Gen());
test(new ConcurrentLinkedQueue<String>(), new Genx());
test(new LinkedBlockingQueue<String>(), new Gen());
test(new LinkedBlockingQueue<String>(), new Genx());
test(new PriorityBlockingQueue<String>(), new Gen());
test(new PriorityBlockingQueue<String>(), new Genx());
}
}