开发中的异常收集(持续更新中...)

Posted haxiaowei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开发中的异常收集(持续更新中...)相关的知识,希望对你有一定的参考价值。

java.lang.ArithmeticException: divide by zero

算数异常,除数为0

 java.util.concurrent.RejectedExecutionException: pool=128/128, queue=10/10

AsyncTask引发的异常,线程池已满拒绝过剩task

默认的线程池会引发这个异常

    AsyncTask.class
...
 public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                    TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);


...
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }
...
    private static final RejectedExecutionHandler defaultHandler =
        new AbortPolicy();
...
    public static class AbortPolicy implements RejectedExecutionHandler {
        /**
         * Creates an {@code AbortPolicy}.
         */
        public AbortPolicy() { }

        /**
         * Always throws RejectedExecutionException.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         * @throws RejectedExecutionException always
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }
...

  解决办法:自定义 RejectedExecutionHandler或者调用其他实现类

  public static final Executor THREAD_POOL_EXECUTOR
            = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
            TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory, new ThreadPoolExecutor.DiscardOldestPolicy());

  

 

以上是关于开发中的异常收集(持续更新中...)的主要内容,如果未能解决你的问题,请参考以下文章

Android开发人员不得不收集的代码(持续更新中)

活动到片段通信:当我尝试从活动更新片段中的文本视图时,出现空指针异常

回归 | js实用代码片段的封装与总结(持续更新中...)

小程序各种功能代码片段整理---持续更新

Tomcat异常及解决办法——持续更新中

java领域优质电子书收集(持续更新)