KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > PooledThread


1 package org.sapia.ubik.net;
2
3
4 /**
5  * Implements a pooled thread. Inheriting classes need only implementing
6  * the <code>doExec</code> template method.
7  *
8  * @see ThreadPool
9  * @see #doExec(Object)
10  *
11  * @author Yanick Duchesne
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
14  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
15  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
16  * </dl>
17  */

18 public abstract class PooledThread extends Thread JavaDoc {
19   private Pool _pool;
20   private Object JavaDoc _task;
21   private boolean _shutdown;
22   private boolean _aquired;
23
24   protected PooledThread() {
25   }
26
27   /** Sets the pool that owns this thread */
28   final void setOwner(Pool pool) {
29     _pool = pool;
30   }
31
32   /**
33    * Sets this pool's task, which represents a unit of work to
34    * perform. This wakes up the thread, which then calls
35    * its own <code>doExec</code> method.
36    *
37    * @see #doExec(Object)
38    */

39   public final synchronized void exec(Object JavaDoc task) {
40     _task = task;
41     notify();
42   }
43
44   /**
45    * Stops this thread.
46    */

47   public void shutdown() {
48     _shutdown = true;
49     interrupt();
50   }
51
52   boolean isShutdown() {
53     return _shutdown;
54   }
55
56   void acquire() {
57     _aquired = true;
58   }
59
60   void release() {
61     _aquired = false;
62   }
63
64   public final synchronized void run() {
65     while (true) {
66       while ((_task == null) && !_shutdown) {
67         try {
68           wait();
69         } catch (InterruptedException JavaDoc e) {
70           _task = null;
71
72           if (_aquired) {
73             _pool.release(this);
74
75             return;
76           }
77         }
78       }
79
80       if (_shutdown) {
81         _task = null;
82
83         if (_aquired) {
84           _pool.release(this);
85         }
86
87         return;
88       }
89
90       try {
91         doExec(_task);
92       } catch (Exception JavaDoc e) {
93         // noop
94
}
95
96       _task = null;
97       _pool.release(this);
98     }
99   }
100
101   /**
102    * Executes the "task" passed in, which is an arbitrary
103    * application-specific unit of work that this method performs. In
104    * fact, it will probably be, in most cases, some data on which this
105    * method's implementation will perform some actions. If the
106    * object passed in is eventually shared between multiple threads,
107    * it should provide a proper thread-safe behavior.
108    * <p>
109    * This template method is to be implemented by subclasses.
110    *
111    * @param a task to execute, or data on which this method should act.
112    */

113   protected abstract void doExec(Object JavaDoc task);
114 }
115
Popular Tags