KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > test > AbstractConcurrentStressTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.tm.test;
23
24 import java.util.ArrayList JavaDoc;
25
26 import org.jboss.test.JBossTestCase;
27
28 /**
29  * Abstract concurrent stress test.
30  *
31  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
32  * @version $Revision: 37406 $
33  */

34 public class AbstractConcurrentStressTest extends JBossTestCase
35 {
36    private ArrayList JavaDoc done = new ArrayList JavaDoc();
37    private int total;
38    
39    public interface ConcurrentTestCallback
40    {
41       void finished() throws Throwable JavaDoc;
42    }
43    
44    public void runConcurrentTest(ConcurrentRunnable[] runnables, ConcurrentTestCallback callback) throws Throwable JavaDoc
45    {
46       total = runnables.length;
47       Thread JavaDoc[] threads = new Thread JavaDoc[total];
48       for (int i = 0; i < total; ++i)
49       {
50          threads[i] = new Thread JavaDoc(runnables[i], getName() + "-" + i);
51          threads[i].start();
52       }
53       for (int i = 0; i < total; ++i)
54          threads[i].join();
55
56       if (callback != null)
57          callback.finished();
58       
59       for (int i = 0; i < total; ++i)
60          runnables[i].doCheck();
61    }
62    
63    public abstract class ConcurrentRunnable implements Runnable JavaDoc
64    {
65       public Throwable JavaDoc failure;
66       
67       public abstract void doStart();
68       public abstract void doRun();
69       public abstract void doEnd();
70       public void doCheck() throws Throwable JavaDoc
71       {
72          if (failure != null)
73             throw failure;
74       }
75       
76       public void run()
77       {
78          doStart();
79          waitDone();
80          for (int i =0; i < getIterationCount(); ++i)
81             doRun();
82          waitDone();
83          doEnd();
84          waitDone();
85       }
86    }
87
88    protected synchronized void waitDone()
89    {
90       if (done.size() < total - 1)
91       {
92          done.add(this);
93          doWait();
94       }
95       else
96       {
97          for (int i = 0; i < done.size(); ++i)
98             done.get(i).notify();
99          done.clear();
100       }
101    }
102
103    protected void doWait()
104    {
105       boolean interrupted = false;
106       try
107       {
108          while (true)
109          {
110             try
111             {
112                wait();
113                return;
114             }
115             catch (InterruptedException JavaDoc e)
116             {
117                interrupted = true;
118             }
119          }
120       }
121       finally
122       {
123          if (interrupted)
124             Thread.currentThread().interrupt();
125       }
126       
127    }
128    
129    public AbstractConcurrentStressTest(String JavaDoc name)
130    {
131       super(name);
132    }
133 }
134
Popular Tags