| 1 |
/* Worker.java */ |
| 2 |
package org.xlattice.util.threads; |
| 3 |
|
| 4 |
/** |
| 5 |
* A thread for use in a thread pool. |
| 6 |
* |
| 7 |
* @author Jim Dixon |
| 8 |
*/ |
| 9 |
public class Worker extends Thread implements Killable { |
| 10 |
|
| 11 |
// INSTANCE VARIABLES /////////////////////////////////////////// |
| 12 |
protected final ThreadList workers; |
| 13 |
protected final JobQueue jobQueue; |
| 14 |
|
| 15 |
/** thread is running a job */ |
| 16 |
private volatile boolean busy; |
| 17 |
/** kill has been requested */ |
| 18 |
private volatile boolean dying; |
| 19 |
|
| 20 |
// CONSTRUCTORS ///////////////////////////////////////////////// |
| 21 |
/** |
| 22 |
* Create a Worker, a carrier for jobs to be run in a thread pool. |
| 23 |
*/ |
| 24 |
public Worker (ThreadGroup tg, String name, |
| 25 |
ThreadList tl, JobQueue jobs, boolean daemon) { |
| 26 |
super (tg, name); |
| 27 |
workers = tl; |
| 28 |
jobQueue = jobs; |
| 29 |
this.setDaemon(daemon); // otherwise no interest in this flag |
| 30 |
} |
| 31 |
|
| 32 |
// PROPERTIES /////////////////////////////////////////////////// |
| 33 |
/** @return whether the thread is busy */ |
| 34 |
protected final boolean isBusy() { |
| 35 |
return busy; |
| 36 |
} |
| 37 |
/** @return whether the thread is dying */ |
| 38 |
protected final boolean isDying() { |
| 39 |
return dying; |
| 40 |
} |
| 41 |
// INTERFACE Killable /////////////////////////////////////////// |
| 42 |
/** |
| 43 |
* Mark this thread as dying. |
| 44 |
* |
| 45 |
* Implementation: the thread may be either waiting in the |
| 46 |
* while loop for a job or it may be actually running the job. |
| 47 |
* |
| 48 |
* In the first case, set dying, the variable tested |
| 49 |
* in run()'s while loop, and then actually interrupt the thread. |
| 50 |
* This will take an unpredictable amount of time to have an effect, |
| 51 |
* but eventually the thread will exit the monitor, check the |
| 52 |
* control variable, and do its termination routine. |
| 53 |
* |
| 54 |
* Jobs using this set of thread pool classes should be designed |
| 55 |
* to accept this pattern. They should either respond in a |
| 56 |
* sensible way to interrupts, or should periodically inspect a |
| 57 |
* publicly accessible control variable, or do both. |
| 58 |
*/ |
| 59 |
public final synchronized void die() { |
| 60 |
dying = true; |
| 61 |
//myThread.interrupt(); |
| 62 |
} |
| 63 |
// INTERFACE Runnable (from Thread) ///////////////////////////// |
| 64 |
/** If there is a job to run, run it. */ |
| 65 |
public final void run() { |
| 66 |
while (!dying) { |
| 67 |
runNextJob(); |
| 68 |
} |
| 69 |
// morituri te salutamus |
| 70 |
synchronized (workers) { |
| 71 |
// XXX THIS CREATES PROBLEMS; can affect busyCount |
| 72 |
workers.remove(this); |
| 73 |
workers.notify(); // wake up the guy who told me to die |
| 74 |
} |
| 75 |
} |
| 76 |
/** |
| 77 |
* Split out to allow easy subclassing. |
| 78 |
*/ |
| 79 |
protected void runNextJob() { |
| 80 |
Runnable job = null; |
| 81 |
synchronized (jobQueue) { |
| 82 |
while (jobQueue.size() == 0 && !dying) { |
| 83 |
try { |
| 84 |
synchronized (jobQueue) { jobQueue.wait(); } |
| 85 |
} catch (InterruptedException ie) { |
| 86 |
// ignored, but forces test of the while condition |
| 87 |
} |
| 88 |
} |
| 89 |
// still within jobQueue monitor, restarted by notify() |
| 90 |
if (!dying && jobQueue.size() > 0) { |
| 91 |
job = (Runnable) jobQueue.dequeue(); |
| 92 |
} |
| 93 |
} |
| 94 |
if (job != null && !dying) { |
| 95 |
synchronized(workers) { |
| 96 |
workers.startJob(); // increments count of busy jobs |
| 97 |
busy = true; |
| 98 |
} |
| 99 |
job.run(); |
| 100 |
synchronized(workers) { |
| 101 |
busy = false; |
| 102 |
workers.endJob(); // decrements count |
| 103 |
} |
| 104 |
job = null; // GC is good for you |
| 105 |
} |
| 106 |
} |
| 107 |
} |