01: package org.methodize.nntprss.util;
02:
03: /* -----------------------------------------------------------
04: * nntp//rss - a bridge between the RSS world and NNTP clients
05: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
06: *
07: * email: nntprss@methodize.org
08: * mail: Methodize Solutions
09: * PO Box 3865
10: * Grand Central Station
11: * New York NY 10163
12: *
13: * This file is part of nntp//rss
14: *
15: * nntp//rss is free software; you can redistribute it
16: * and/or modify it under the terms of the GNU General
17: * Public License as published by the Free Software Foundation;
18: * either version 2 of the License, or (at your option) any
19: * later version.
20: *
21: * This program is distributed in the hope that it will be
22: * useful, but WITHOUT ANY WARRANTY; without even the implied
23: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
24: * PURPOSE. See the GNU General Public License for more
25: * details.
26: *
27: * You should have received a copy of the GNU General Public
28: * License along with this program; if not, write to the
29: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
30: * Boston, MA 02111-1307 USA
31: * ----------------------------------------------------- */
32:
33: /**
34: * @author Jason Brome <jason@methodize.org>
35: * @version $Id: WorkerThread.java,v 1.2 2003/01/22 05:12:15 jasonbrome Exp $
36: */
37: public class WorkerThread extends Thread {
38:
39: private boolean active = true;
40: private Runnable job = null;
41: private SimpleThreadPool threadPool = null;
42:
43: public WorkerThread(ThreadGroup threadGroup,
44: SimpleThreadPool threadPool, String threadName) {
45: super (threadGroup, (Runnable) null, threadName);
46: this .threadPool = threadPool;
47: }
48:
49: public synchronized void run(Runnable newJob) {
50: if (this .job != null) {
51: throw new IllegalStateException(
52: "Thread is already handling job");
53: } else {
54: this .job = newJob;
55: this .notify();
56: }
57: }
58:
59: public synchronized void end() {
60: active = false;
61: this .notify();
62: }
63:
64: /**
65: * @see java.lang.Runnable#run()
66: */
67: public void run() {
68: while (active) {
69: // We have work to do?
70: if (job != null) {
71: try {
72: job.run();
73: } catch (Exception e) {
74: // Need to log the exception..
75: }
76: }
77:
78: job = null;
79:
80: // Return thread to the pool
81: threadPool.makeThreadAvailable(this );
82:
83: // Wait for next job
84: synchronized (this ) {
85: while (active && job == null) {
86: try {
87: wait();
88: } catch (InterruptedException e) {
89: }
90: }
91: }
92:
93: }
94: }
95:
96: }
|