01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.org
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.pool.thread;
09:
10: import org.huihoo.jfox.pool.ObjectPool;
11: import org.huihoo.jfox.pool.SimpleObjectPool;
12: import org.huihoo.jfox.pool.ObjectFactory;
13: import org.huihoo.jfox.service.ComponentSupport;
14:
15: /**
16: *
17: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
18: */
19:
20: public class SimpleThreadPool extends ComponentSupport implements
21: SimpleThreadPoolMBean, ThreadPool {
22:
23: /**
24: * the objectPool to cache the WorkerThread object
25: */
26: private ObjectPool pool = null;
27:
28: public SimpleThreadPool() {
29: try {
30: pool = new SimpleObjectPool(new ThreadableObjectFactory(
31: WorkerThread.class), 5, 10);
32: } catch (Exception e) {
33: logger.fatal(e.getMessage(), e);
34: }
35: }
36:
37: public void runThread(Threadable task) {
38: try {
39: WorkerThread thread = (WorkerThread) pool.retrieveObject();
40: ThreadTask threadTask = new ThreadTask(task, pool);
41: thread.setTask(threadTask);
42: } catch (Exception e) {
43: logger.error(e.getMessage(), e);
44: }
45: }
46:
47: protected void doInit() throws Exception {
48: try {
49: pool.init();
50: } catch (Exception e) {
51: logger.fatal("object pool instantiator error", e);
52: // e.printStackTrace();
53: }
54: }
55:
56: protected void doDestroy() throws Exception {
57: pool.destroy();
58: }
59:
60: public void clear() {
61: pool.clear();
62: }
63:
64: public ObjectFactory getObjectFactory() {
65: return pool.getObjectFactory();
66: }
67:
68: public String getObjectClass() {
69: return pool.getObjectClass();
70: }
71:
72: public int getWorking() {
73: return pool.getWorking();
74: }
75:
76: public int getRest() {
77: return pool.getRest();
78: }
79:
80: }
|