KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > mbean2 > MTTest


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.mbean2;
23
24 import org.jboss.system.ServiceMBeanSupport;
25 import org.jboss.test.tm.resource.MTOperation;
26
27 /**
28  * Server Side MultiThread TM test.
29  *
30  * Based on mbean.TMTest
31  *
32  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
33  * @version $Revision: 37406 $
34  */

35 public class MTTest extends ServiceMBeanSupport
36    implements MTTestMBean
37 {
38    public void testMTOperations(String JavaDoc test, MTOperation[][] ops) throws Exception JavaDoc
39    {
40       log.info("*** Starting test: " + test);
41       MTOperation.init(log);
42       
43       // find out how many MTOperation[]
44
// we'll execute each in a separate thread
45
int numOfThreads = ops.length;
46       log.info("Number of Threads: " + numOfThreads);
47          
48       Thread JavaDoc[] threads = new Thread JavaDoc[numOfThreads];
49       ExecTask[] tasks = new ExecTask[numOfThreads];
50       for (int i = 0; i < numOfThreads; i++)
51       {
52          tasks[i] = new ExecTask(i, ops[i]);
53          threads[i] = new Thread JavaDoc(tasks[i]);
54          threads[i].start();
55       }
56       
57       // join the threads before returning and check
58
// if any of threads exited with an exception
59
Exception JavaDoc caughtException = null;
60       for (int i = 0; i < numOfThreads; i++)
61       {
62          try
63          {
64             threads[i].join();
65             if (tasks[i].exception != null)
66             {
67                // remember any exception caught; order here is not important,
68
// since we don't know which thread finished first.
69
caughtException = tasks[i].exception;
70             }
71          }
72          catch (InterruptedException JavaDoc e)
73          {
74             // retry
75
i--;
76          }
77       }
78       log.info("*** Finished test: " + test);
79       MTOperation.destroy();
80       
81       if (caughtException != null)
82       {
83          throw caughtException;
84       }
85    }
86    
87    private class ExecTask implements Runnable JavaDoc
88    {
89       int threadId;
90       MTOperation[] ops;
91       Exception JavaDoc exception;
92       
93       public ExecTask(int threadId, MTOperation[] ops)
94       {
95          this.threadId = threadId;
96          this.ops = ops;
97       }
98       
99       public void run()
100       {
101          log.info("Starting thread: " + Thread.currentThread().getName());
102          try
103          {
104             for (int i = 0; i < ops.length; ++i)
105             {
106                ops[i].perform();
107             }
108          }
109          catch (Exception JavaDoc e)
110          {
111             exception = e;
112          }
113          finally
114          {
115             log.info("Finished thread: " + Thread.currentThread().getName());
116          }
117       }
118    }
119 }
120
Popular Tags