1 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 18 19 public class SimpleThreadPool extends AbstractComponent implements ThreadPool { 20 21 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 e) { 36 logger.error(e.getMessage(), e); 37 } 38 } 39 40 protected void doInit() throws Exception { 41 try { 42 pool = new SimpleObjectPool(new ThreadObjectFactory(PoolableThreadImpl.class), 5, 10); 43 pool.init(); 44 } 45 catch(Exception e) { 46 logger.fatal("object pool instantiator error", e); 47 } 49 } 50 51 protected void doDestroy() throws Exception { 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 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 { 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 |