您的位置:首页 > 编程语言 > Java开发

Custom ThreadPool in Java to Perform as ThreadPool in C# based on Singleton Model

2013-03-21 10:24 519 查看
Every process has a default ThreadPool with defalut size 25 in .Net application development. It is very convenient as writing ThreadPool.QueueUserWorkItem(CallBack);

As so far, I have not found a default tool class to do the same thing in JDK1.6.

So, I try to complete one based on Singleton Model.

public class CustomThreadPool{

private BlockingQueue<Runnable> workQueue;
private ThreadPoolExecutor poolExecutor;
pirvate static CustomThreadPool customThreadPool;

private CustomThreadPool(){
if(workQueue == null){
workQueue = new LinkedBlockingDeque<Runnbale>();
}
if(poolExecutor == null){
poolExecutor = new ThreadPoolExecutor(5, 25, 1, TimeUnit.DAYS, workQueue);
}
}

public static void queueUserWorkItem(Runnble workItem){
if(customThreadPool == null){
customThreadPool = new CustomThreadPoll();
}
customThreadPool.poolExecutor.execute(workItem);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐