View Javadoc
1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.threads;
48
49 import com.rhi.architecture.threads.ThreadPool;
50
51 /***
52 * Worker Threads performs a given work and sleeps after the work is finished.
53 *
54 * The thread is managed by a ThreadPool and is used in conjuction with a
55 * ThreadPool.
56 *
57 * The threads sleeps as long as there is no work to be performed.
58 * ThreadPool assigns work to the thread and wakes up the WorkerThread to
59 * perform the given work. Once the thread finishes the work it sets itself
60 * to as having no work.
61 *
62 * @author <a href="mailto:Chandra.Ilindra@rhi.com">Chandra Ilindra</a>
63 * @copyright 2002, Robert Half Int'l. All rights reserved.
64 *
65 * @version 1.0
66 */
67 public class WorkerThread extends Thread {
68
69 // reference to ThreadPool managing this worker thread.
70 private ThreadPool pool = null;
71
72 // assigned work
73 private Runnable work = null;
74 // indicates if the thread is marked for shutdown by the ThreadPool.
75 private boolean shutDown = false;
76
77 /***
78 * Constructor - constructs a WorkerThread
79 *
80 * @param pool - reference to the managing ThreadPool
81 *
82 * @since 1.0
83 */
84 public WorkerThread(ThreadPool pool) {
85 this.pool = pool;
86 setDaemon(true);
87 }
88
89 /***
90 * Perform the following steps, in a loop, until asked to shutdown:
91 * 1. If there is no work then go to sleep ( wait ).
92 * 2. If there is work:
93 * - perform the work
94 * - reset work to null
95 * 3. Exit the loop, if asked to shutdown.
96 *
97 * @since 1.0
98 */
99 public void run() {
100
101 while (true) {
102 if (noWork()) {
103 try {
104 synchronized (this) {
105 wait();
106 }
107 }
108 catch (InterruptedException e) {
109 // ignore
110 }
111 }
112 else {
113 work.run();
114 setNoWork();
115
116 }
117
118 if (shutDown) {
119 break;
120 }
121 }
122
123 }
124
125 /***
126 * return true is there is no work to be performed.
127 * @return boolean
128 * @since 1.0
129 */
130 public boolean noWork() {
131 return work == null;
132 }
133
134 /***
135 * assign no work to the worked thread.
136 *
137 * @since 1.0
138 */
139 public void setNoWork() {
140 work = null;
141 }
142
143 /***
144 * mark the thread to be shutdown.
145 *
146 * @since 1.0
147 */
148 public void markForShutDown() {
149 shutDown = true;
150 }
151
152 /***
153 * Sets the work to be performed by the thread.
154 *
155 * @param action - action to be performed by the thread
156 *
157 * @since 1.0
158 */
159 public void setWork(Runnable action) {
160 work = action;
161 }
162 }
This page was automatically generated by Maven