1 18 package org.apache.batik.util; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Random ; 23 24 import org.apache.batik.test.AbstractTest; 25 import org.apache.batik.test.TestReport; 26 27 public class RunnableQueueTest extends AbstractTest { 28 29 public int nThreads; 30 public int activeThreads; 31 public Random rand; 32 public RunnableQueue rq; 33 34 40 public RunnableQueueTest(int nThreads) { 41 this.nThreads = nThreads; 42 } 43 public RunnableQueueTest(Integer nThreads) { 44 this((nThreads==null)?10:nThreads.intValue()); 45 } 46 47 50 public String getName() { 51 return "RunnableQueue Stress Test"; 52 } 53 54 58 public TestReport runImpl() throws Exception { 59 rq = RunnableQueue.createRunnableQueue(); 60 61 List l = new ArrayList (nThreads); 62 rand = new Random (2345); 63 64 l.add(new SwitchFlicker()); 66 l.add(new SwitchFlicker()); 67 68 for (int i=0; i<nThreads; i++) { 69 Runnable rqRable = new RQRable(i, rand.nextInt(50)+1); 70 l.add(new TPRable(rq, i, rand.nextInt(4)+1, 71 rand.nextInt(500)+1, 20, rqRable)); 72 } 73 74 synchronized (this) { 75 ThreadPounder tp = new ThreadPounder(l); 76 tp.start(); 77 activeThreads = nThreads; 78 while (activeThreads != 0) { 79 wait(); 80 } 81 } 82 83 System.exit(0); 84 return null; 85 } 86 87 public class SwitchFlicker implements Runnable { 88 public void run() { 89 boolean suspendp, waitp; 90 int time; 91 while (true) { 92 try { 93 synchronized (rand) { 94 suspendp = rand.nextBoolean(); 95 waitp = rand.nextBoolean(); 96 time = rand.nextInt(500); 97 } 98 if (suspendp) { 99 rq.suspendExecution(waitp); 102 System.out.println("Suspended - " + 103 (waitp?"Wait":"Later")); 104 Thread.sleep(time/10); 105 } else { 106 rq.resumeExecution(); 108 System.out.println("Resumed"); 109 Thread.sleep(time); 110 } 111 } catch(InterruptedException ie) { } 112 } 113 } 114 } 115 116 public static final int INVOKE_LATER = 1; 117 public static final int INVOKE_AND_WAIT = 2; 118 public static final int PREEMPT_LATER = 3; 119 public static final int PREEMPT_AND_WAIT = 4; 120 121 public class TPRable implements Runnable { 122 123 RunnableQueue rq; 124 int idx; 125 int style; 126 long repeatDelay; 127 int count; 128 Runnable rqRable; 129 130 TPRable(RunnableQueue rq, int idx, 131 int style, 132 long repeatDelay, int count, 133 Runnable rqRable) { 134 this.rq = rq; 135 this.idx = idx; 136 this.style = style; 137 this.repeatDelay = repeatDelay; 138 this.count = count; 139 this.rqRable = rqRable; 140 } 141 142 public void run() { 143 try { 144 while (count-- != 0) { 145 switch (style) { 146 case INVOKE_LATER: 147 synchronized (rqRable) { 148 System.out.println(" InvL #" + idx); 149 rq.invokeLater(rqRable); 150 System.out.println("Done InvL #" + idx); 151 rqRable.wait(); 152 } 153 break; 154 case INVOKE_AND_WAIT: 155 System.out.println(" InvW #" + idx); 156 rq.invokeAndWait(rqRable); 157 System.out.println("Done InvW #" + idx); 158 break; 159 case PREEMPT_LATER: 160 synchronized (rqRable) { 161 System.out.println(" PreL #" + idx); 162 rq.preemptLater(rqRable); 163 System.out.println("Done PreL #" + idx); 164 rqRable.wait(); 165 } 166 break; 167 case PREEMPT_AND_WAIT: 168 System.out.println(" PreW #" + idx); 169 rq.preemptAndWait(rqRable); 170 System.out.println("Done PreW #" + idx); 171 break; 172 } 173 174 if (repeatDelay < 0) 175 break; 176 Thread.sleep(repeatDelay); 177 } 178 } catch (InterruptedException ie) { 179 } 180 synchronized(RunnableQueueTest.this) { 181 activeThreads--; 182 RunnableQueueTest.this.notify(); 183 } 184 } 185 } 186 187 public static class RQRable implements Runnable { 188 int idx; 189 long dur; 190 191 RQRable(int idx, long dur) { 192 this.idx = idx; 193 this.dur = dur; 194 } 195 196 public void run() { 197 try { 198 System.out.println(" B Rable #" + idx); 199 Thread.sleep(dur); 200 System.out.println(" E Rable #" + idx); 201 synchronized (this) { 202 notify(); 203 } 204 } catch (InterruptedException ie) { } 205 } 206 } 207 208 public static void main(String []args) { 209 RunnableQueueTest rqt = new RunnableQueueTest(20); 210 try { 211 rqt.runImpl(); 212 } catch (Exception e) { 213 e.printStackTrace(); 214 } 215 } 216 } 217 | Popular Tags |