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 ; 10 11 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 msg) throws Exception { 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 o); 42 Object removeElement(); 43 } 44 45 class Adder extends Thread { 46 public void run() { 47 for(int i=0; i < NUM; i++) { 48 try { 49 q.addElement(new Integer (i)); 50 } 53 catch(Exception e) { 54 e.printStackTrace(); 55 } 56 } 57 } 59 } 60 61 class Remover extends Thread { 62 int i=0; 63 public void run() { 64 do { 65 try { 66 q.removeElement(); 67 i++; 68 } 71 catch(Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 while(i < NUM); 76 stop=System.currentTimeMillis(); 77 } 79 } 80 81 82 public static class JgQueue extends Queue implements Queueable { 83 84 public void addElement(Object o) { 85 try { 86 add(o); 87 } 88 catch(QueueClosedException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 public Object 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 o) { 108 try { 109 add(o); 110 } 111 catch(QueueClosedException e) { 112 e.printStackTrace(); 113 } 114 } 115 116 public Object 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 implements Queueable { 130 Object mutex=new Object (); 131 boolean waiting=false; 133 public void addElement(Object o) { 134 synchronized(mutex) { 135 super.add(o); 136 if(waiting) 137 mutex.notifyAll(); } 139 } 140 141 142 public Object 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 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 o) { 168 try { 169 super.put(o); 170 } 171 catch(InterruptedException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 public Object removeElement() { 177 try { 178 return super.take(); 179 } 180 catch(InterruptedException 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 o) { 190 try { 191 super.put(o); 192 } 193 catch(InterruptedException e) { 194 e.printStackTrace(); 195 } 196 } 197 198 public Object removeElement() { 199 try { 200 return super.take(); 201 } 202 catch(InterruptedException e) { 203 e.printStackTrace(); 204 return null; 205 } 206 } 207 } 208 209 210 public static void main(String [] 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 t) { 230 t.printStackTrace(); 231 } 232 } 233 234 } 235 | Popular Tags |