001: package org.jgroups.tests;
002:
003: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
004: import EDU.oswego.cs.dl.util.concurrent.WaitFreeQueue;
005: import org.jgroups.util.Queue;
006: import org.jgroups.util.Queue2;
007: import org.jgroups.util.QueueClosedException;
008:
009: import java.util.LinkedList;
010:
011: /**
012: *
013: * @author bela
014: * Date: Jul 25, 2003
015: * Time: 2:14:32 PM
016: */
017: public class QueueTest2 {
018: Queueable q = null;
019:
020: long start, stop;
021: long NUM = 1000 * 1000;
022:
023: void start(Queueable q, String msg) throws Exception {
024: this .q = q;
025: System.out.println("-- starting test with " + q.getClass()
026: + " (" + msg + ')');
027: start = System.currentTimeMillis();
028: Adder adder = new Adder();
029: Remover remover = new Remover();
030: remover.start();
031: adder.start();
032: adder.join();
033: remover.join();
034: System.out.println("-- done with " + q.getClass());
035: System.out.println(" total time for " + NUM + " elements: "
036: + (stop - start) + " msecs\n\n");
037: }
038:
039: public interface Queueable {
040: void addElement(Object o);
041:
042: Object removeElement();
043: }
044:
045: class Adder extends Thread {
046: public void run() {
047: for (int i = 0; i < NUM; i++) {
048: try {
049: q.addElement(new Integer(i));
050: //if(i % 1000 == 0)
051: // System.out.println("-- added " + i);
052: } catch (Exception e) {
053: e.printStackTrace();
054: }
055: }
056: //System.out.println("-- Adder: done");
057: }
058: }
059:
060: class Remover extends Thread {
061: int i = 0;
062:
063: public void run() {
064: do {
065: try {
066: q.removeElement();
067: i++;
068: //if(i % 1000 == 0)
069: // System.out.println("-- removed " + i);
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: } while (i < NUM);
074: stop = System.currentTimeMillis();
075: // System.out.println("-- Remover: done");
076: }
077: }
078:
079: public static class JgQueue extends Queue implements Queueable {
080:
081: public void addElement(Object o) {
082: try {
083: add(o);
084: } catch (QueueClosedException e) {
085: e.printStackTrace();
086: }
087: }
088:
089: public Object removeElement() {
090: try {
091: return remove();
092: } catch (QueueClosedException e) {
093: e.printStackTrace();
094: return null;
095: }
096: }
097: }
098:
099: public static class JgQueue2 extends Queue2 implements Queueable {
100:
101: public void addElement(Object o) {
102: try {
103: add(o);
104: } catch (QueueClosedException e) {
105: e.printStackTrace();
106: }
107: }
108:
109: public Object removeElement() {
110: try {
111: return remove();
112: } catch (QueueClosedException e) {
113: e.printStackTrace();
114: return null;
115: }
116: }
117: }
118:
119: public static class MyQueue extends LinkedList implements Queueable {
120: Object mutex = new Object();
121: boolean waiting = false; // remover waiting on mutex
122:
123: public void addElement(Object o) {
124: synchronized (mutex) {
125: super .add(o);
126: if (waiting)
127: mutex.notifyAll(); // todo: change to notify()
128: }
129: }
130:
131: public Object removeElement() {
132: synchronized (mutex) {
133: if (size() > 0) {
134: return removeFirst();
135: } else {
136: waiting = true;
137: try {
138: mutex.wait();
139: return removeFirst();
140: } catch (InterruptedException e) {
141: e.printStackTrace();
142: return null;
143: } finally {
144: waiting = false;
145: }
146: }
147: }
148: }
149: }
150:
151: public static class MyLinkedQueue extends LinkedQueue implements
152: Queueable {
153: public void addElement(Object o) {
154: try {
155: super .put(o);
156: } catch (InterruptedException e) {
157: e.printStackTrace();
158: }
159: }
160:
161: public Object removeElement() {
162: try {
163: return super .take();
164: } catch (InterruptedException e) {
165: e.printStackTrace();
166: return null;
167: }
168: }
169: }
170:
171: public static class MyWaitFreeQueue extends WaitFreeQueue implements
172: Queueable {
173: public void addElement(Object o) {
174: try {
175: super .put(o);
176: } catch (InterruptedException e) {
177: e.printStackTrace();
178: }
179: }
180:
181: public Object removeElement() {
182: try {
183: return super .take();
184: } catch (InterruptedException e) {
185: e.printStackTrace();
186: return null;
187: }
188: }
189: }
190:
191: public static void main(String[] args) {
192: try {
193: QueueTest2 qt = new QueueTest2();
194:
195: Queueable q = new JgQueue();
196: qt.start(q, "based on org.jgroups.util.Queue");
197:
198: q = new JgQueue2();
199: qt
200: .start(q,
201: "based on org.jgroups.util.Queue2 (using util.concurrent Mutexes and CondVars)");
202:
203: q = new MyQueue();
204: qt.start(q, "based on java.util.LinkedList");
205:
206: q = new MyLinkedQueue();
207: qt
208: .start(q,
209: "based on EDU.oswego.cs.dl.util.concurrent.LinkedQueue");
210:
211: q = new MyWaitFreeQueue();
212: qt
213: .start(q,
214: "based on EDU.oswego.cs.dl.util.concurrent.WaitFreeQueue");
215: } catch (Throwable t) {
216: t.printStackTrace();
217: }
218: }
219:
220: }
|