KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > threading > WorkerThread


1 package net.javacoding.jspider.core.threading;
2
3
4 import net.javacoding.jspider.core.logging.LogFactory;
5 import net.javacoding.jspider.core.logging.Log;
6 import net.javacoding.jspider.core.task.WorkerTask;
7
8
9 /**
10  * Implementation of a Worker Thread.
11  * This thread will accept WorkerTasks and execute them.
12  *
13  * $Id: WorkerThread.java,v 1.11 2003/04/02 20:55:26 vanrogu Exp $
14  *
15  * @author Günther Van Roey
16  */

17 class WorkerThread extends Thread JavaDoc {
18
19     public static final int WORKERTHREAD_IDLE = 0;
20     public static final int WORKERTHREAD_BLOCKED = 1;
21     public static final int WORKERTHREAD_BUSY = 2;
22
23
24     /** the current state of this thread - idle, blocked, or busy. */
25     protected int state;
26
27     /** Whether this instance is assigned a task. */
28     protected boolean assigned;
29
30     /** Whether we should keep alive this thread. */
31     protected boolean running;
32
33     /** Threadpool this worker is part of. */
34     protected WorkerThreadPool stp;
35
36     /** Task this worker is assigned to. */
37     protected WorkerTask task;
38
39     /**
40      * Public constructor.
41      * @param stp thread pool this worker is part of
42      * @param name name of the thread
43      * @param i index in the pool
44      */

45     public WorkerThread(WorkerThreadPool stp, String JavaDoc name, int i) {
46         super(stp, name + " " + i);
47         this.stp = stp;
48         running = false;
49         assigned = false;
50         state = WORKERTHREAD_IDLE;
51     }
52
53     /**
54      * Tests whether this worker thread instance can be assigned a task.
55      * @return whether we're capable of handling a task.
56      */

57     public boolean isAvailable() {
58         return (!assigned) && running;
59     }
60
61     /**
62      * Determines whether we're occupied.
63      * @return boolean value representing our occupation
64      */

65     public boolean isOccupied() {
66         return assigned;
67     }
68
69     /**
70      * Method that allows the threadPool to assign the worker a Task.
71      * @param task WorkerTask to be executed.
72      */

73     public synchronized void assign(WorkerTask task) {
74         if ( !running ) {
75             //SHOULDN'T HAPPEN WITHOUT BUGS
76
throw new RuntimeException JavaDoc("THREAD NOT RUNNING, CANNOT ASSIGN TASK !!!");
77         }
78         if (assigned) {
79             //SHOULDN'T HAPPEN WITHOUT BUGS
80
throw new RuntimeException JavaDoc("THREAD ALREADY ASSIGNED !!!");
81         }
82         this.task = task;
83         assigned = true;
84         notify();
85     }
86
87     /**
88      * Tells this thread not to accept any new tasks.
89      */

90     public synchronized void stopRunning() {
91         if ( ! running ) {
92             throw new RuntimeException JavaDoc ("THREAD NOT RUNNING - CANNOT STOP !");
93         }
94         if ( assigned ) {
95             try {
96                 this.wait();
97             } catch (InterruptedException JavaDoc e) {
98                 Thread.currentThread().interrupt();
99             }
100         }
101         running = false;
102         notify();
103     }
104
105     /**
106      * Returns the state of this worker thread (idle, blocked or busy).
107      * @return
108      */

109     public int getState ( ) {
110         return state;
111     }
112
113     /**
114      * Thread's overridden run method.
115      */

116     public synchronized void run() {
117         running = true;
118
119         Log log = LogFactory.getLog(WorkerThread.class);
120         log.debug("Worker thread (" + this.getName() + ") born");
121
122         synchronized (stp) {
123             stp.notify();
124         }
125
126         while (running) {
127             if (assigned) {
128                 state = WORKERTHREAD_BLOCKED;
129                 task.prepare();
130                 state = WORKERTHREAD_BUSY;
131                 try {
132                     task.execute();
133                     task.tearDown();
134                 } catch (Exception JavaDoc e) {
135                     log.fatal("PANIC! Task " + task + " threw an excpetion!", e);
136                     System.exit(1);
137                 }
138
139                 synchronized (stp) {
140                     assigned = false;
141                     task = null;
142                     state = WORKERTHREAD_IDLE;
143                     stp.notify();
144                     this.notify(); // if some thread is blocked in stopRunning();
145
}
146
147             }
148             try {
149                 wait();
150             } catch (InterruptedException JavaDoc e) {
151                 Thread.currentThread().interrupt();
152             }
153         }
154
155         /* notify the thread pool that we died. */
156         log.debug("Worker thread (" + this.getName() + ") dying");
157     }
158 }
159
Popular Tags