KickJava   Java API By Example, From Geeks To Geeks.

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


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

16
17 package org.apache.batik.util;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Random JavaDoc;
24
25 /**
26  * The purpose of this class is to invoke a series of runnables as
27  * closely to synchronously as possible. It does this by starting
28  * a thread for each one, getting the threads into there run method,
29  * then quickly running through (in random order) and notifying each
30  * thread.
31  */

32 public class ThreadPounder {
33     List JavaDoc runnables;
34     Object JavaDoc [] threads;
35     Object JavaDoc lock = new Object JavaDoc();
36
37     public ThreadPounder(List JavaDoc runnables)
38         throws InterruptedException JavaDoc {
39         this(runnables, new Random JavaDoc(1234));
40     }
41
42     public ThreadPounder(List JavaDoc runnables, Random JavaDoc rand)
43         throws InterruptedException JavaDoc {
44         this.runnables = new ArrayList JavaDoc(runnables);
45         Collections.shuffle(this.runnables, rand);
46         threads = new Object JavaDoc[this.runnables.size()];
47         int i=0;
48         Iterator JavaDoc iter= this.runnables.iterator();
49         synchronized (lock) {
50             while (iter.hasNext()) {
51                 Thread JavaDoc t = new SyncThread((Runnable JavaDoc)iter.next());
52                 t.start();
53                 lock.wait();
54                 threads[i] = t;
55                 i++;
56             }
57         }
58     }
59
60     public void start() {
61         synchronized(this) {
62             this.notifyAll();
63         }
64
65     }
66
67     class SyncThread extends Thread JavaDoc {
68         Runnable JavaDoc toRun;
69         public long runTime;
70         public SyncThread(Runnable JavaDoc toRun) {
71             this.toRun = toRun;
72         }
73
74         public void run() {
75             try {
76                 synchronized (ThreadPounder.this) {
77                     synchronized (lock) {
78                         // Let pounder know I'm ready to go
79
lock.notify();
80                     }
81                     // Wait for pounder to wake me up.
82
ThreadPounder.this.wait();
83                 }
84                 toRun.run();
85             } catch (InterruptedException JavaDoc ie) {
86             }
87         }
88     }
89
90     public static void main(String JavaDoc [] str) {
91         List JavaDoc l = new ArrayList JavaDoc(20);
92         for (int i=0; i<20; i++) {
93             final int x = i;
94             l.add(new Runnable JavaDoc() {
95                     public void run() {
96                         System.out.println("Thread " + x);
97                     }
98                 });
99         }
100
101         try {
102             ThreadPounder tp = new ThreadPounder(l);
103             System.out.println("Starting:" );
104             tp.start();
105             System.out.println("All Started:" );
106         } catch (InterruptedException JavaDoc ie) {
107             ie.printStackTrace();
108         }
109     }
110 }
111
Popular Tags