1 16 17 package org.apache.jetspeed.services.threadpool; 18 19 import org.apache.turbine.services.TurbineServices; 21 22 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 24 import org.apache.jetspeed.services.logging.JetspeedLogger; 25 26 35 public class RunnableThread extends Thread 36 { 37 40 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RunnableThread.class.getName()); 41 42 private boolean running = false; 43 private static int next_id = 0; 44 45 49 private static synchronized int getNextId() { 50 return ++next_id; 51 }; 52 private int id = 0; 53 54 57 private Runnable runnable = null; 58 59 public RunnableThread() 60 { 61 super(); 62 this.setDaemon(true); 63 } 64 65 70 public RunnableThread( ThreadGroup tg) { 71 super( tg, "Provisory"); 72 this.id = getNextId(); 73 super.setName("RunnableThread:" + this.id ); 74 this.setPriority( Thread.MIN_PRIORITY ); 75 this.setDaemon(true); 76 77 } 78 79 86 public RunnableThread( ThreadGroup tg, 87 int id ) { 88 89 super( tg, "RunnableThread:" + id ); 90 this.setPriority( Thread.MIN_PRIORITY ); 91 this.setDaemon(true); 92 this.id = id; 93 } 94 95 99 public void run() { 100 101 104 boolean poolrunning = true; 105 while ( poolrunning ) { 106 107 108 this.setRunning( false ); 113 114 this.setRunnable( null ); 115 116 synchronized( this ) { 117 if( this.getPriority() != 118 JetspeedThreadPoolService.DEFAULT_THREAD_PRIORITY ) { 119 this.setPriority( JetspeedThreadPoolService.DEFAULT_THREAD_PRIORITY ); 121 } 122 123 128 132 ( (JetspeedThreadPoolService)TurbineServices 134 .getInstance() 135 .getService( ThreadPoolService.SERVICE_NAME ) ) 136 .release( this ); 137 138 139 if ( this.getRunnable() == null ) { 142 143 try { 144 this.wait(); 145 } catch (InterruptedException e) { 146 } catch ( Throwable t ) { 150 logger.error("Throwable", t); 151 } 153 154 } 155 156 } 157 158 159 if ( this.getRunnable() != null ) { 160 this.setRunning( true ); 161 162 try { 163 164 this.getRunnable().run(); 165 166 } catch ( Throwable t ) { 167 logger.error( "A problem occured while trying to run your thread", t ); 168 } 169 170 } 171 172 } 173 174 } 175 176 178 183 public void setRunnable( Runnable runnable ) { 184 this.runnable = runnable; 185 } 186 187 192 public Runnable getRunnable() { 193 return this.runnable; 194 } 195 196 202 private boolean isRunning() { 203 return this.running; 204 } 205 206 211 private void setRunning( boolean running ) { 212 this.running = running; 213 } 214 215 220 public int getId() { 221 return this.id; 222 } 223 } 224 | Popular Tags |