KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > threadpool > RunnableTaskWrapper


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.util.threadpool;
23
24 import org.jboss.logging.Logger;
25
26 /**
27  * Makes a runnable a task.
28  *
29  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
30  * @version $Revision: 1958 $
31  */

32 public class RunnableTaskWrapper implements TaskWrapper
33 {
34    // Constants -----------------------------------------------------
35

36    /** The log */
37    private static final Logger log = Logger.getLogger(RunnableTaskWrapper.class);
38
39    // Attributes ----------------------------------------------------
40

41    /** The runnable */
42    private Runnable JavaDoc runnable;
43    private boolean started;
44    private Thread JavaDoc runThread;
45    /** The start timeout */
46    private long startTimeout;
47    /** The completion timeout */
48    private long completionTimeout;
49
50    // Static --------------------------------------------------------
51

52    // Constructors --------------------------------------------------
53

54    /**
55     * Create a new RunnableTaskWrapper
56     *
57     * @param runnable the runnable
58     * @throws IllegalArgumentException for a null runnable
59     */

60    public RunnableTaskWrapper(Runnable JavaDoc runnable)
61    {
62       this(runnable, 0, 0);
63    }
64    public RunnableTaskWrapper(Runnable JavaDoc runnable, long startTimeout, long completeTimeout)
65    {
66       if (runnable == null)
67          throw new IllegalArgumentException JavaDoc("Null runnable");
68       this.runnable = runnable;
69       this.startTimeout = startTimeout;
70       this.completionTimeout = completeTimeout;
71    }
72
73    // Public --------------------------------------------------------
74

75    // TaskWrapper implementation ---------------------------------------
76

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       // Nothing to do
100
}
101
102    public void rejectTask(RuntimeException JavaDoc t)
103    {
104       throw t;
105    }
106
107    public void stopTask()
108    {
109       boolean trace = log.isTraceEnabled();
110       // Interrupt the run thread if its not null
111
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          /* If the thread has not been returned after being interrupted, then
120          use the deprecated stop method to try to force the thread abort.
121          */

122          runThread.stop();
123          if( trace )
124             log.trace("stopTask, stopped thread="+runThread);
125       }
126    }
127
128    public void waitForTask()
129    {
130       // Nothing to do
131
}
132
133    public boolean isComplete()
134    {
135       return started == true && runThread == null;
136    }
137    // Runnable implementation ---------------------------------------
138

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 JavaDoc t)
154       {
155          log.warn("Unhandled throwable for runnable: " + runnable, t);
156       }
157    }
158
159    // Package protected ---------------------------------------------
160

161    // Protected -----------------------------------------------------
162

163    // Private -------------------------------------------------------
164

165    // Inner classes -------------------------------------------------
166
}
167
Popular Tags