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

java线程池

阅读更多
public class PoolAsynService extends BaseService implements Runnable {
	private Thread thread = new Thread(this);
	private List waitToList = (List) Collections.synchronizedList(new LinkedList());

	// ////////////线程池参数/////////////////
	private int corePoolSize = 5;// : 线程池维护线程的最少数量
	private int maximumPoolSize = 10;// :线程池维护线程的最大数量
	private long keepAliveTime = 60;// : 线程池维护线程所允许的空闲时间
	private TimeUnit unit = TimeUnit.SECONDS;// : 线程池维护线程所允许的空闲时间的单位
	private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(10);// :
	// 线程池所使用的缓冲队列
	private RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();// :
	// 线程池对拒绝任务的处理策略
	// //////////线程池参数/////////////////
	private ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize,
			maximumPoolSize, keepAliveTime, unit, workQueue, handler);

	public void run() {
		while (!thread.isInterrupted()) {
			if (!waitToList.isEmpty()) {
				try {
					threadPool.execute(new Executor());
				} catch (Exception e) {
					logger.error("pool  execute error!!!", e);
				}

				
			}
			
			Tools.block(25);

		}

	}

	public void doAsync(Object executor, Object... objects) {
		Throwable t = new Throwable();
		StackTraceElement[] elements = t.getStackTrace();
		StackTraceElement element = elements[1];
		String method = element.getMethodName();
		AsyncContext ctx = new AsyncContext();
		ctx.args = objects;
		ctx.executor = executor;
		ctx.method = method;
		if (method.endsWith("PA")) {
			waitToList.add(ctx);
		} else {
			logger.warn("async method name is not good!");
		}

	}

	private class AsyncContext {
		String method;
		Object executor;
		Object[] args;

	}

	private class Executor implements Runnable {
		public void run() {
			if (!waitToList.isEmpty()) {
				try {
					Object task = waitToList.remove(0);
					AsyncContext ctx = (AsyncContext) task;
					doTaskByCtx(ctx);
				} catch (Exception e) {
					logger.error("async error!!!", e);

				}
			}
		}

		private void doTaskByCtx(AsyncContext ctx) {
			String targetMethodName = ctx.method.substring(0, ctx.method
					.length() - 2);
			Method targetMethod = null;
			Class clazz = null;
			try {
				clazz = ctx.executor.getClass();
				Method[] methods = clazz.getDeclaredMethods();

				if (methods != null) {
					for (int i = 0; i < methods.length; i++) {
						String name = methods[i].getName();
						if (name.equals(targetMethodName)) {
							targetMethod = methods[i];
							break;
						}
					}

					if (targetMethod != null) {
						targetMethod.invoke(ctx.executor, ctx.args);
					}

				}
			} catch (Exception e) {
				logger.error(
						"do async fail! " + clazz + ":" + targetMethodName, e);
			}
		}
	}

	@Override
	public void destroy() {
		thread.interrupt();
		threadPool.shutdown();
		logger.info("thread pool asynService shut down");

	}

	@Override
	public void init() {
		thread.start();
		logger.info("thread pool asynService start");

	}

}
分享到:
评论
1 楼 kaneg 2008-07-17  
请楼主格式化下代码,用code标签
xxxxx
,这样看起来舒服些

相关推荐

Global site tag (gtag.js) - Google Analytics