KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > MultiThreadTestRunner


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test;
10
11 import java.io.PrintStream JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * @version $Revision: 1.3 $
18  */

19 public class MultiThreadTestRunner
20 {
21    public abstract static class Test
22    {
23       private List JavaDoc throwables = new ArrayList JavaDoc();
24
25       public abstract void test() throws Exception JavaDoc;
26
27       private synchronized void addThrowable(Throwable JavaDoc x)
28       {
29          throwables.add(x);
30       }
31
32       private synchronized Throwable JavaDoc[] getThrowables()
33       {
34          return (Throwable JavaDoc[])throwables.toArray(new Throwable JavaDoc[throwables.size()]);
35       }
36    }
37
38    private class MultiThrowable extends Exception JavaDoc
39    {
40       private final Throwable JavaDoc[] throwables;
41
42       public MultiThrowable(Throwable JavaDoc[] throwables)
43       {
44          this.throwables = throwables;
45       }
46
47       public void printStackTrace(PrintStream JavaDoc stream)
48       {
49          synchronized (stream)
50          {
51             stream.println(this);
52             for (int i = 0; i < throwables.length; ++i) throwables[i].printStackTrace(stream);
53          }
54       }
55
56       public void printStackTrace(PrintWriter JavaDoc writer)
57       {
58          synchronized (writer)
59          {
60             writer.println(this);
61             for (int i = 0; i < throwables.length; ++i) throwables[i].printStackTrace(writer);
62          }
63       }
64    }
65
66    private final int threads;
67    private final int iterations;
68
69    public MultiThreadTestRunner(int threads, int iterations)
70    {
71       this.threads = threads;
72       this.iterations = iterations;
73    }
74
75    public void run(final Test test) throws Exception JavaDoc
76    {
77       Thread JavaDoc[] runners = new Thread JavaDoc[threads];
78       for (int i = 0; i < threads; ++i)
79       {
80          runners[i] = new Thread JavaDoc(new Runnable JavaDoc()
81          {
82             public void run()
83             {
84                for (int i = 0; i < iterations; ++i)
85                {
86                   try
87                   {
88                      test.test();
89                   }
90                   catch (Throwable JavaDoc x)
91                   {
92                      test.addThrowable(x);
93                   }
94                }
95             }
96          });
97       }
98
99       for (int i = 0; i < threads; ++i) runners[i].start();
100
101       for (int i = 0; i < threads; ++i) runners[i].join();
102
103       Throwable JavaDoc[] failures = test.getThrowables();
104       if (failures != null && failures.length > 0) throw new MultiThrowable(failures);
105    }
106 }
107
Popular Tags