/**
* 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));
}
}
}