package org.jboss.util.threadpool;
import org.jboss.logging.Logger;
public class RunnableTaskWrapper implements TaskWrapper
{
private static final Logger log = Logger.getLogger(RunnableTaskWrapper.class);
private Runnable runnable;
private boolean started;
private Thread runThread;
private long startTimeout;
private long completionTimeout;
public RunnableTaskWrapper(Runnable runnable)
{
this(runnable, 0, 0);
}
public RunnableTaskWrapper(Runnable runnable, long startTimeout, long completeTimeout)
{
if (runnable == null)
throw new IllegalArgumentException("Null runnable");
this.runnable = runnable;
this.startTimeout = startTimeout;
this.completionTimeout = completeTimeout;
}
public int getTaskWaitType()
{
return Task.WAIT_NONE;
}
public int getTaskPriority()
{
return Thread.NORM_PRIORITY;
}
public long getTaskStartTimeout()
{
return startTimeout;
}
public long getTaskCompletionTimeout()
{
return completionTimeout;
}
public void acceptTask()
{
}
public void rejectTask(RuntimeException t)
{
throw t;
}
public void stopTask()
{
boolean trace = log.isTraceEnabled();
if( runThread != null && runThread.isInterrupted() == false )
{
runThread.interrupt();
if( trace )
log.trace("stopTask, interrupted thread="+runThread);
}
else if( runThread != null )
{
runThread.stop();
if( trace )
log.trace("stopTask, stopped thread="+runThread);
}
}
public void waitForTask()
{
}
public boolean isComplete()
{
return started == true && runThread == null;
}
public void run()
{
boolean trace = log.isTraceEnabled();
try
{
if( trace )
log.trace("Begin run, wrapper="+this);
runThread = Thread.currentThread();
started = true;
runnable.run();
runThread = null;
if( trace )
log.trace("End run, wrapper="+this);
}
catch (Throwable t)
{
log.warn("Unhandled throwable for runnable: " + runnable, t);
}
}
}