KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > RunnableQueueTest


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Random JavaDoc;
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 JavaDoc rand;
32     public RunnableQueue rq;
33
34     /**
35      * Constructor
36      * @param nThreads number of runnables to queue
37      * @param sync Should requests be made synchronously (from
38      * different threads).
39      */

40     public RunnableQueueTest(int nThreads) {
41         this.nThreads = nThreads;
42     }
43     public RunnableQueueTest(Integer JavaDoc nThreads) {
44         this((nThreads==null)?10:nThreads.intValue());
45     }
46         
47         /**
48          * Returns this Test's name
49          */

50         public String JavaDoc getName() {
51             return "RunnableQueue Stress Test";
52         }
53
54     /**
55      * This method will only throw exceptions if some aspect
56      * of the test's internal operation fails.
57      */

58     public TestReport runImpl() throws Exception JavaDoc {
59         rq = RunnableQueue.createRunnableQueue();
60
61         List JavaDoc l = new ArrayList JavaDoc(nThreads);
62         rand = new Random JavaDoc(2345);
63
64         // Two switch flickers to make things interesting...
65
l.add(new SwitchFlicker());
66         l.add(new SwitchFlicker());
67
68         for (int i=0; i<nThreads; i++) {
69             Runnable JavaDoc 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 JavaDoc {
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                         // 1/2 of the time suspend, 1/2 time wait, 1/2 the
100
// time don't
101
rq.suspendExecution(waitp);
102                         System.out.println("Suspended - " +
103                                            (waitp?"Wait":"Later"));
104                         Thread.sleep(time/10);
105                     } else {
106                         // 1/2 the time resume
107
rq.resumeExecution();
108                         System.out.println("Resumed");
109                         Thread.sleep(time);
110                     }
111                 } catch(InterruptedException JavaDoc 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 JavaDoc {
122
123         RunnableQueue rq;
124         int idx;
125         int style;
126         long repeatDelay;
127         int count;
128         Runnable JavaDoc rqRable;
129
130         TPRable(RunnableQueue rq, int idx,
131                 int style,
132                 long repeatDelay, int count,
133                 Runnable JavaDoc 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 JavaDoc ie) {
179             }
180             synchronized(RunnableQueueTest.this) {
181                 activeThreads--;
182                 RunnableQueueTest.this.notify();
183             }
184         }
185     }
186
187     public static class RQRable implements Runnable JavaDoc {
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 JavaDoc ie) { }
205         }
206     }
207
208     public static void main(String JavaDoc []args) {
209         RunnableQueueTest rqt = new RunnableQueueTest(20);
210         try {
211             rqt.runImpl();
212         } catch (Exception JavaDoc e) {
213             e.printStackTrace();
214         }
215     }
216 }
217
Popular Tags