一、前言
留给自己的前言:这个线程池在运用中非常有用,能够捕获异常是非常重要的,否则在此期间很难发现你的异常在哪里提交了;
二 自己定义扩展线程池
1、首先构建一个基础的 执行线程
package jDKMulit; import java.util.concurrent.*; /** * Created by ycy on 16/1/12. */ public class DviTask implements Runnable{ int a,b; public void run() { double re=a/b; System.out.println(re); } public DviTask(int a,int b){ this.a=a; this.b=b; } public static void main(String[] args) throws ExecutionException, InterruptedException { ThreadPoolExecutor pools= new ThreadPoolExecutor(0,Integer.MAX_VALUE,0l, TimeUnit.SECONDS,new SynchronousQueue<Runnable>()); for (int i = 0; i <5 ; i++) { // pools.execute(new DviTask(100,i)); Future re= pools.submit(new DviTask(100,i)); re.get(); } } }
2、扩展我们的线程池
package jDKMulit; import java.util.concurrent.*; /** * Created by ycy on 16/1/13. * 自己扩展线程池获取异常位置 * 重写submit:可以拉取异常 */ public class VeryTraceThreadPoolExecutor extends ThreadPoolExecutor { // 初始化 public VeryTraceThreadPoolExecutor(int corePoolSize, int maxmumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue){ super(corePoolSize,maxmumPoolSize,keepAliveTime,unit,workQueue); } //执行方法 @Override public void execute(Runnable command) { super.execute(command); } @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() { public void run() { try { task.run(); } catch (Exception e) { clientStack.printStackTrace(); try { throw e; } catch (Exception e1) { e1.printStackTrace(); } } } }; } public static void main(String[] args) { ThreadPoolExecutor pools=new VeryTraceThreadPoolExecutor(0,Integer.MAX_VALUE,0l,TimeUnit.SECONDS,new SynchronousQueue<Runnable>()); //错误堆栈中可以看到是在哪里提交的任务 for (int i = 0; i < 5; i++) { pools.execute(new DviTask(100,i)); } } }
3、执行结果查询异常位置
异常查看;不仅仅可以得到异常发生Runnable的时限内的信息,而且得到了我们任务在哪里提交(红字体);对于排除错误很重要
<span style="color:#ff0000;">java.lang.Exception: client stack trace</span> at jDKMulit.VeryTraceThreadPoolExecutor.clientTrace(VeryTraceThreadPoolExecutor.java:31) at jDKMulit.VeryTraceThreadPoolExecutor.execute(VeryTraceThreadPoolExecutor.java:21) at jDKMulit.VeryTraceThreadPoolExecutor.main(VeryTraceThreadPoolExecutor.java:57) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) java.lang.ArithmeticException: / by zero at jDKMulit.DviTask.run(DviTask.java:12) at jDKMulit.VeryTraceThreadPoolExecutor$1.run(VeryTraceThreadPoolExecutor.java:40) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) 100.0 25.0 33.0 50.0