/**
* Copyright (c) 2009 - 2010 - OpenFARM
* Licence: MIT
*
* @Author Filipe Martins
* @Date 11-May-2010
*/
package openfarm.multithreading.threadpools;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import openfarm.interpreter.InterpreterRegister;
import openfarm.properties.PropertiesReader;
import org.apache.log4j.Logger;
import util.logging.LogUtil;
public class GeneralThreadPool implements IThreadPool
{
private static final Logger log = LogUtil.getLogger(GeneralThreadPool.class);
protected int poolSize;
protected int maxPoolSize;
protected long keepAliveTime;
protected ThreadPoolExecutor threadPool;
private PropertiesReader prop;
/**The properties: poolSize, maxPoolSize and keepAliveTime must be specified in the interpreter.properties file. This is because only the client knows
* how many CPU's he has in his box. If these values are not assigned the system will default to 10*/
public GeneralThreadPool()
{
try
{
this.prop = new PropertiesReader(InterpreterRegister.PROPERTIES_FILE);
} catch (IOException e)
{
log.error("Could not load..." + InterpreterRegister.PROPERTIES_FILE);
}
this.poolSize = prop.getProperty("poolSize") != null ? Integer.parseInt(prop.getProperty("poolSize")) : 10;
this.maxPoolSize = prop.getProperty("maxPoolsize") != null ? Integer.parseInt(prop.getProperty("maxPoolsize")) : 10;
this.keepAliveTime = prop.getProperty("keepAliveTime") != null ? Integer.parseInt(prop.getProperty("keepAliveTime")) : 10;
this.threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10));
}
public void addTaskToPool(Runnable task)
{
threadPool.execute(task);
}
public void shutdown()
{
this.threadPool.shutdown();
}
public ThreadPoolExecutor getThreadPoolExecutor()
{
return threadPool;
}
}
|