`
hezhiyu
  • 浏览: 60968 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

关于线程池的代码,常会用到

 
阅读更多
package com.tuan.partner.client.impl;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;

import com.tuan.partner.client.util.WoWoLogger;

public class ThreadPoolManager {
	private static Log logger = WoWoLogger.getLog(ThreadPoolManager.class);

	private static final int CORE_POOL_SIZE = 20;

	private static final int MAX_POOL_SIZE = 50;

	private static final long KEEP_ALIVE_TIME = 0;

	private static final int WORK_QUEUE_SIZE = 1000;

	private static ThreadPoolManager threadPoolManager = new ThreadPoolManager();

	final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
			CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
			new ArrayBlockingQueue(WORK_QUEUE_SIZE),
			new ThreadPoolExecutor.CallerRunsPolicy());

	public static ThreadPoolManager newInstance() {
		return threadPoolManager;
	}

	public void execute(Runnable command) {
		logger.debug("execute thread");
		this.threadPool.execute(command);
	}

	public BlockingQueue getQueue() {
		return this.threadPool.getQueue();
	}

	public ThreadPoolExecutor getPool() {
		return this.threadPool;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics