1 22 package org.jboss.test.classloader.concurrentload; 23 24 import java.util.HashSet ; 25 import java.util.Timer ; 26 import java.util.TimerTask ; 27 import java.util.Vector ; 28 29 import org.jboss.logging.Logger; 30 import org.jboss.system.Service; 31 import org.jboss.system.ServiceMBeanSupport; 32 33 34 40 public class ConcurrentLoader 41 extends ServiceMBeanSupport 42 implements ConcurrentLoaderMBean 43 { 44 45 47 49 public Object lock = new Object (); 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 classes = new HashSet (); 55 private Vector ungarbaged = new Vector (); 56 private Timer newInstanceTimer; 57 private int doneCount; 58 59 61 63 public ConcurrentLoader () 64 { 65 } 66 67 69 71 73 protected void createService() throws Exception 74 { 75 log.debug("Creating "+NUMBER_OF_THREADS+" threads..."); 76 newInstanceTimer = new Timer (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 106 { 107 newInstanceTimer.cancel(); 108 classes.clear(); 109 ungarbaged.clear(); 110 } 111 112 114 116 118 120 class NewInstanceTask extends TimerTask 121 { 122 Logger theLog; 123 NewInstanceTask() 124 { 125 this.theLog = ConcurrentLoader.this.getLog(); 126 } 127 128 130 public void run() 131 { 132 int size = classes.size(); 133 Class [] theClasses = new Class [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 clazz = theClasses[c]; 141 Object obj = clazz.newInstance(); 142 theLog.debug("Created instance="+obj); 143 } 144 catch(Throwable t) 145 { 146 t.printStackTrace(); 147 } 148 } 149 } 150 } 151 152 class Loader extends Thread 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 className = this.getClass ().getPackage ().getName () + ".Anyclass" + modId; 168 ClassLoader cl = this.getContextClassLoader (); 169 170 synchronized (lock) 171 { 172 try 173 { 174 theLog.debug("Thread ready: " + classid); 175 lock.wait (); 176 } 177 catch (Exception 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 clazz = cl.loadClass (className); 192 classes.add(clazz); 193 Object obj = null; theLog.debug("Class " + className + " loaded, obj="+obj); 195 } 196 catch (Throwable 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 |