KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > QueueTest2


1 package org.jgroups.tests;
2
3 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
4 import EDU.oswego.cs.dl.util.concurrent.WaitFreeQueue;
5 import org.jgroups.util.Queue;
6 import org.jgroups.util.Queue2;
7 import org.jgroups.util.QueueClosedException;
8
9 import java.util.LinkedList JavaDoc;
10
11 /**
12  *
13  * @author bela
14  * Date: Jul 25, 2003
15  * Time: 2:14:32 PM
16  */

17 public class QueueTest2 {
18     Queueable q=null;
19
20
21
22     long start, stop;
23     long NUM=1000 * 1000;
24
25     void start(Queueable q, String JavaDoc msg) throws Exception JavaDoc {
26         this.q=q;
27         System.out.println("-- starting test with " + q.getClass() + " (" + msg + ')');
28         start=System.currentTimeMillis();
29         Adder adder=new Adder();
30         Remover remover=new Remover();
31         remover.start();
32         adder.start();
33         adder.join();
34         remover.join();
35         System.out.println("-- done with " + q.getClass());
36         System.out.println(" total time for " + NUM + " elements: " + (stop-start) + " msecs\n\n");
37     }
38
39
40     public interface Queueable {
41         void addElement(Object JavaDoc o);
42         Object JavaDoc removeElement();
43     }
44
45     class Adder extends Thread JavaDoc {
46         public void run() {
47             for(int i=0; i < NUM; i++) {
48                 try {
49                     q.addElement(new Integer JavaDoc(i));
50                     //if(i % 1000 == 0)
51
// System.out.println("-- added " + i);
52
}
53                 catch(Exception JavaDoc e) {
54                     e.printStackTrace();
55                 }
56             }
57             //System.out.println("-- Adder: done");
58
}
59     }
60
61     class Remover extends Thread JavaDoc {
62         int i=0;
63         public void run() {
64             do {
65                 try {
66                     q.removeElement();
67                     i++;
68                     //if(i % 1000 == 0)
69
// System.out.println("-- removed " + i);
70
}
71                 catch(Exception JavaDoc e) {
72                     e.printStackTrace();
73                 }
74             }
75             while(i < NUM);
76             stop=System.currentTimeMillis();
77             // System.out.println("-- Remover: done");
78
}
79     }
80
81
82     public static class JgQueue extends Queue implements Queueable {
83
84         public void addElement(Object JavaDoc o) {
85             try {
86                 add(o);
87             }
88             catch(QueueClosedException e) {
89                 e.printStackTrace();
90             }
91         }
92
93         public Object JavaDoc removeElement() {
94             try {
95                 return remove();
96             }
97             catch(QueueClosedException e) {
98                 e.printStackTrace();
99                 return null;
100             }
101         }
102     }
103
104
105         public static class JgQueue2 extends Queue2 implements Queueable {
106
107         public void addElement(Object JavaDoc o) {
108             try {
109                 add(o);
110             }
111             catch(QueueClosedException e) {
112                 e.printStackTrace();
113             }
114         }
115
116         public Object JavaDoc removeElement() {
117             try {
118                 return remove();
119             }
120             catch(QueueClosedException e) {
121                 e.printStackTrace();
122                 return null;
123             }
124         }
125     }
126
127
128
129     public static class MyQueue extends LinkedList JavaDoc implements Queueable {
130         Object JavaDoc mutex=new Object JavaDoc();
131         boolean waiting=false; // remover waiting on mutex
132

133         public void addElement(Object JavaDoc o) {
134             synchronized(mutex) {
135                 super.add(o);
136                 if(waiting)
137                     mutex.notifyAll(); // todo: change to notify()
138
}
139         }
140
141
142         public Object JavaDoc removeElement() {
143             synchronized(mutex) {
144                 if(size() > 0) {
145                     return removeFirst();
146                 }
147                 else {
148                     waiting=true;
149                     try {
150                         mutex.wait();
151                         return removeFirst();
152                     }
153                     catch(InterruptedException JavaDoc e) {
154                         e.printStackTrace();
155                         return null;
156                     }
157                     finally {
158                         waiting=false;
159                     }
160                 }
161             }
162         }
163     }
164
165
166     public static class MyLinkedQueue extends LinkedQueue implements Queueable {
167         public void addElement(Object JavaDoc o) {
168             try {
169                 super.put(o);
170             }
171             catch(InterruptedException JavaDoc e) {
172                 e.printStackTrace();
173             }
174         }
175
176         public Object JavaDoc removeElement() {
177             try {
178                 return super.take();
179             }
180             catch(InterruptedException JavaDoc e) {
181                 e.printStackTrace();
182                 return null;
183             }
184         }
185     }
186
187
188       public static class MyWaitFreeQueue extends WaitFreeQueue implements Queueable {
189         public void addElement(Object JavaDoc o) {
190             try {
191                 super.put(o);
192             }
193             catch(InterruptedException JavaDoc e) {
194                 e.printStackTrace();
195             }
196         }
197
198         public Object JavaDoc removeElement() {
199             try {
200                 return super.take();
201             }
202             catch(InterruptedException JavaDoc e) {
203                 e.printStackTrace();
204                 return null;
205             }
206         }
207     }
208
209
210     public static void main(String JavaDoc[] args) {
211         try {
212             QueueTest2 qt=new QueueTest2();
213
214             Queueable q=new JgQueue();
215             qt.start(q, "based on org.jgroups.util.Queue");
216
217             q=new JgQueue2();
218             qt.start(q, "based on org.jgroups.util.Queue2 (using util.concurrent Mutexes and CondVars)");
219
220             q=new MyQueue();
221             qt.start(q, "based on java.util.LinkedList");
222
223             q=new MyLinkedQueue();
224             qt.start(q, "based on EDU.oswego.cs.dl.util.concurrent.LinkedQueue");
225
226             q=new MyWaitFreeQueue();
227             qt.start(q, "based on EDU.oswego.cs.dl.util.concurrent.WaitFreeQueue");
228         }
229         catch(Throwable JavaDoc t) {
230             t.printStackTrace();
231         }
232     }
233
234 }
235
Popular Tags