KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > thread > SimpleThreadPool


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.pool.thread;
8
9 import org.jfox.ioc.common.AbstractComponent;
10 import org.jfox.pool.AbstractObjectPool;
11 import org.jfox.pool.ObjectFactory;
12 import org.jfox.pool.PoolableObject;
13 import org.jfox.pool.SimpleObjectPool;
14
15 /**
16  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
17  */

18
19 public class SimpleThreadPool extends AbstractComponent implements ThreadPool {
20
21     /**
22      * the objectPool to cache the PoolableThreadImpl object
23      */

24     private AbstractObjectPool pool = null;
25
26     public SimpleThreadPool() {
27
28     }
29
30     public void execute(Threadable task) {
31         try {
32             ThreadTask threadTask = new ThreadTask(task, pool);
33             ((PoolableThread) pool.retrieveObject()).setTask(threadTask);
34         }
35         catch(Exception JavaDoc e) {
36             logger.error(e.getMessage(), e);
37         }
38     }
39
40     protected void doInit() throws Exception JavaDoc {
41         try {
42             pool = new SimpleObjectPool(new ThreadObjectFactory(PoolableThreadImpl.class), 5, 10);
43             pool.init();
44         }
45         catch(Exception JavaDoc e) {
46             logger.fatal("object pool instantiator error", e);
47 // e.printStackTrace();
48
}
49     }
50
51     protected void doDestroy() throws Exception JavaDoc {
52         pool.destroy();
53     }
54
55     public void clear() {
56         pool.clear();
57     }
58
59     public ObjectFactory getObjectFactory() {
60         return pool.getObjectFactory();
61     }
62
63     public String JavaDoc getObjectClass() {
64         return pool.getObjectClass();
65     }
66
67     public int getWorking() {
68         return pool.getWorking();
69     }
70
71     public int getRest() {
72         return pool.getRest();
73     }
74
75     public PoolableObject retrieveObject() throws Exception JavaDoc {
76         return pool.retrieveObject();
77     }
78
79     public boolean restoreObject(PoolableObject obj) {
80         return pool.restoreObject(obj);
81     }
82
83     public boolean removeObject(PoolableObject obj) {
84         return pool.removeObject(obj);
85     }
86
87 }
Popular Tags