ThreadPoolExecutor 暴露异常


/**
 * Created By Corleone
 * Created at 2018年2月4日 下午6:06:42
 */
package thread_pool;

/**
 * @author Corleone
 * @created at 2018年2月4日下午6:06:42
 */
public class DivTask implements Runnable {
	int a, b;

	public DivTask(int a, int b) {
		this.a = a;
		this.b = b;
	}

	@Override
	public void run() {
		double d = a / b;
		System.out.println("a/b is:" + d);

	}

}

/**
 * Created By Corleone
 * Created at 2018年2月4日 下午5:55:27
 */
package thread_pool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Corleone
 * @created at 2018年2月4日下午5:55:27
 */
public class TraceThreadPoolExecutor extends ThreadPoolExecutor {

	/**
	 * @param corePoolSize
	 * @param maximumPoolSize
	 * @param keepAliveTime
	 * @param unit
	 * @param workQueue
	 * @param threadFactory
	 * @param handler
	 */
	public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
	}

	/**
	 * @param corePoolSize
	 * @param maximumPoolSize
	 * @param keepAliveTime
	 * @param unit
	 * @param workQueue
	 * @param threadFactory
	 */
	public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
	}

	/**
	 * @param corePoolSize
	 * @param maximumPoolSize
	 * @param keepAliveTime
	 * @param unit
	 * @param workQueue
	 */
	public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
	}

	@Override
	public void execute(Runnable task) {
		super.execute(wrap(task, clientTrace(), Thread.currentThread().getName()));
	}

	@Override
	public Future<?> submit(Runnable task) {
		return super.submit(wrap(task, clientTrace(), Thread.currentThread().getName()));
	}

	private Exception clientTrace() {
		return new Exception("client stack trace");
	}

	private Runnable wrap(final Runnable task, final Exception clientStack, String clientThreadName) {
		return new Runnable() {

			@Override
			public void run() {
				try {
					task.run();
				} catch (Exception e) {
					clientStack.printStackTrace();
					throw e;
				}
			}

		};
	}

}

/**
 * Created By Corleone
 * Created at 2018年2月4日 下午6:04:49
 */
package thread_pool;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Corleone
 * @created at 2018年2月4日下午6:04:49
 */
public class TraceThreadPoolExecutorTest {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 5, 300, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

		for (int i = 0; i < 5; i++) {
			pool.submit(new DivTask(100, i));
		}
		// 上述没有错误信息,异常被抛弃了。

		// 将 pool.submit 改成 pool.execute 后,有异常出现。
		ThreadPoolExecutor pool2 = new ThreadPoolExecutor(5, 5, 300, TimeUnit.SECONDS,
				new SynchronousQueue<Runnable>());
		for (int i = 0; i < 5; i++) {
			 pool2.execute(new DivTask(100, i));
		}

		// 使用 Future<?> future 接收结果,也有异常出现。
		ThreadPoolExecutor pool3 = new ThreadPoolExecutor(5, 5, 300, TimeUnit.SECONDS,
				new SynchronousQueue<Runnable>());
		for (int i = 0; i < 5; i++) {
			 Future<?> future = pool3.submit(new DivTask(100, i));
			 future.get();
		}

		// 使用TraceThreadPoolExecutor,之前隐藏掉的异常也抛出来了。
		TraceThreadPoolExecutor pool4 = new TraceThreadPoolExecutor(5, 5, 300, TimeUnit.SECONDS,
				new SynchronousQueue<Runnable>());
		for (int i = 0; i < 5; i++) {
			pool4.execute(new DivTask(100, i));
		}

	}

}

转载于:https://my.oschina.net/corleone/blog/1618245

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值