1 22 package org.jboss.util.threadpool; 23 24 import org.jboss.logging.Logger; 25 26 32 public class RunnableTaskWrapper implements TaskWrapper 33 { 34 36 37 private static final Logger log = Logger.getLogger(RunnableTaskWrapper.class); 38 39 41 42 private Runnable runnable; 43 private boolean started; 44 private Thread runThread; 45 46 private long startTimeout; 47 48 private long completionTimeout; 49 50 52 54 60 public RunnableTaskWrapper(Runnable runnable) 61 { 62 this(runnable, 0, 0); 63 } 64 public RunnableTaskWrapper(Runnable runnable, long startTimeout, long completeTimeout) 65 { 66 if (runnable == null) 67 throw new IllegalArgumentException ("Null runnable"); 68 this.runnable = runnable; 69 this.startTimeout = startTimeout; 70 this.completionTimeout = completeTimeout; 71 } 72 73 75 77 public int getTaskWaitType() 78 { 79 return Task.WAIT_NONE; 80 } 81 82 public int getTaskPriority() 83 { 84 return Thread.NORM_PRIORITY; 85 } 86 87 public long getTaskStartTimeout() 88 { 89 return startTimeout; 90 } 91 92 public long getTaskCompletionTimeout() 93 { 94 return completionTimeout; 95 } 96 97 public void acceptTask() 98 { 99 } 101 102 public void rejectTask(RuntimeException t) 103 { 104 throw t; 105 } 106 107 public void stopTask() 108 { 109 boolean trace = log.isTraceEnabled(); 110 if( runThread != null && runThread.isInterrupted() == false ) 112 { 113 runThread.interrupt(); 114 if( trace ) 115 log.trace("stopTask, interrupted thread="+runThread); 116 } 117 else if( runThread != null ) 118 { 119 122 runThread.stop(); 123 if( trace ) 124 log.trace("stopTask, stopped thread="+runThread); 125 } 126 } 127 128 public void waitForTask() 129 { 130 } 132 133 public boolean isComplete() 134 { 135 return started == true && runThread == null; 136 } 137 139 public void run() 140 { 141 boolean trace = log.isTraceEnabled(); 142 try 143 { 144 if( trace ) 145 log.trace("Begin run, wrapper="+this); 146 runThread = Thread.currentThread(); 147 started = true; 148 runnable.run(); 149 runThread = null; 150 if( trace ) 151 log.trace("End run, wrapper="+this); 152 } 153 catch (Throwable t) 154 { 155 log.warn("Unhandled throwable for runnable: " + runnable, t); 156 } 157 } 158 159 161 163 165 } 167 | Popular Tags |