KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > concurrentload > ConcurrentLoader


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.classloader.concurrentload;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Timer JavaDoc;
26 import java.util.TimerTask JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.jboss.logging.Logger;
30 import org.jboss.system.Service;
31 import org.jboss.system.ServiceMBeanSupport;
32
33
34 /** A multi-threaded class loading test service.
35  *
36  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 1.2
39  */

40 public class ConcurrentLoader
41        extends ServiceMBeanSupport
42        implements ConcurrentLoaderMBean
43 {
44
45    // Constants -----------------------------------------------------
46

47    // Attributes ----------------------------------------------------
48

49    public Object JavaDoc lock = new Object JavaDoc ();
50
51    public final static int MAX_CLASSES = 10;
52    public final static int NUMBER_OF_LOADING = 10;
53    public final static int NUMBER_OF_THREADS = 20;
54    private HashSet JavaDoc classes = new HashSet JavaDoc();
55    private Vector JavaDoc ungarbaged = new Vector JavaDoc();
56    private Timer JavaDoc newInstanceTimer;
57    private int doneCount;
58
59    // Static --------------------------------------------------------
60

61    // Constructors --------------------------------------------------
62

63    public ConcurrentLoader ()
64    {
65    }
66
67    // Public --------------------------------------------------------
68

69    // Z implementation ----------------------------------------------
70

71    // ServiceMBeanSupport overrides ---------------------------------------------------
72

73    protected void createService() throws Exception JavaDoc
74    {
75       log.debug("Creating "+NUMBER_OF_THREADS+" threads...");
76       newInstanceTimer = new Timer JavaDoc(true);
77       newInstanceTimer.scheduleAtFixedRate(new NewInstanceTask(), 0, 100);
78       doneCount = 0;
79       for(int t = 0; t < NUMBER_OF_THREADS; t ++)
80       {
81          ConcurrentLoader.Loader loader = new ConcurrentLoader.Loader (t);
82          loader.start ();
83          ungarbaged.add (loader);
84       }
85       log.info("All threads created");
86       Thread.sleep(2000);
87
88       synchronized (lock)
89       {
90          lock.notifyAll ();
91       }
92        log.info("Unlocked all Loader threads");
93       synchronized( lock )
94       {
95          while( doneCount < NUMBER_OF_THREADS )
96          {
97             lock.wait();
98          }
99          log.info("Loader doneCount="+doneCount);
100       }
101       log.info("All Loaders are done");
102       newInstanceTimer.cancel();
103    }
104
105    protected void stopService() throws Exception JavaDoc
106    {
107       newInstanceTimer.cancel();
108       classes.clear();
109       ungarbaged.clear();
110    }
111
112    // Package protected ---------------------------------------------
113

114    // Protected -----------------------------------------------------
115

116    // Private -------------------------------------------------------
117

118    // Inner classes -------------------------------------------------
119

120    class NewInstanceTask extends TimerTask JavaDoc
121    {
122       Logger theLog;
123       NewInstanceTask()
124       {
125          this.theLog = ConcurrentLoader.this.getLog();
126       }
127
128       /** Create an instance of a class and exit
129        */

130       public void run()
131       {
132          int size = classes.size();
133          Class JavaDoc[] theClasses = new Class JavaDoc[size];
134          classes.toArray(theClasses);
135          theLog.info("NewInstanceTask, creating "+size+" instances");
136          for(int c = 0; c < theClasses.length; c ++)
137          {
138             try
139             {
140                Class JavaDoc clazz = theClasses[c];
141                Object JavaDoc obj = clazz.newInstance();
142                theLog.debug("Created instance="+obj);
143             }
144             catch(Throwable JavaDoc t)
145             {
146                t.printStackTrace();
147             }
148          }
149       }
150    }
151
152    class Loader extends Thread JavaDoc
153    {
154       int classid = 0;
155       Logger theLog;
156
157       public Loader (int classid)
158       {
159          super("ConcurrentLoader - Thread #" + classid);
160          this.classid = classid;
161          this.theLog = ConcurrentLoader.this.getLog();
162       }
163
164       public void run ()
165       {
166          int modId = classid % MAX_CLASSES;
167          String JavaDoc className = this.getClass ().getPackage ().getName () + ".Anyclass" + modId;
168          ClassLoader JavaDoc cl = this.getContextClassLoader ();
169
170          synchronized (lock)
171          {
172             try
173             {
174                theLog.debug("Thread ready: " + classid);
175                lock.wait ();
176             }
177             catch (Exception JavaDoc e)
178             {
179                theLog.error("Error during wait", e);
180             }
181          }
182          theLog.debug("loading class... " + className);
183          for (int i=0; i<NUMBER_OF_LOADING; i++)
184          {
185             theLog.debug("loading class with id " + classid + " for the " + i + "th time");
186             try
187             {
188                theLog.debug("before load...");
189                long sleep = (long) (1000 * Math.random());
190                Thread.sleep(sleep);
191                Class JavaDoc clazz = cl.loadClass (className);
192                classes.add(clazz);
193                Object JavaDoc obj = null; //clazz.newInstance();
194
theLog.debug("Class " + className + " loaded, obj="+obj);
195             }
196             catch (Throwable JavaDoc e)
197             {
198                theLog.debug("Failed to load class and create instance", e);
199             }
200          }
201          theLog.debug("...Done loading classes. " + classid);
202          synchronized( lock )
203          {
204             doneCount ++;
205             lock.notify();
206          }
207       }
208    }
209
210 }
211
Popular Tags