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

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;
}
}

你可能感兴趣的:(java,互联网)