01: /*
02: *
03: * Jsmtpd, Java SMTP daemon
04: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package org.jsmtpd.generic.threadpool;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25:
26: /**
27: * Handles a job
28: * @author Jean-Francois POUX
29: * Jsmtp
30: */
31: public class ThreadWorker extends Thread {
32:
33: private boolean free = false;
34: private boolean running = true;
35: private IThreadedClass wrk;
36: private Log log = LogFactory.getLog(ThreadWorker.class);
37:
38: public void run() {
39: while (true) {
40: goSleep();
41: if (!running) {
42: log.debug("Worker " + getName() + " stopping");
43: return;
44: }
45: try {
46: wrk.doJob();
47: } catch (RuntimeException e) {
48: log.error("Generic Thread pool worker #"
49: + Thread.currentThread().getId()
50: + " Caught a runtime exception : ", e);
51: log.error("Thread job lost, recycling thread");
52: }
53: }
54: }
55:
56: public void forceShutdown() {
57: running = false;
58: free = false;
59: wrk.forceShutdown();
60: wake();
61: }
62:
63: public void gracefullShutdown() {
64: running = false;
65: free = false;
66: wrk.gracefullShutdown();
67: wake();
68: }
69:
70: public void setParam(Object o) {
71: wrk.setParam(o);
72: }
73:
74: public void setWorker(IThreadedClass cl) {
75: wrk = cl;
76: }
77:
78: public boolean isFree() {
79: return free;
80: }
81:
82: public synchronized void wake() {
83: free = false;
84: notify();
85: }
86:
87: private synchronized void goSleep() {
88: free = true;
89: try {
90: wait();
91: } catch (InterruptedException e) {
92: e.printStackTrace();
93: }
94: }
95: }
|