需要帮助理解 'RejectedExecutionExemption'

Need help understanding 'RejectedExecutionExemption'

我是 运行 一些异步任务,我的 1 个用户因 RejectedExecutionExemption 而崩溃:

堆栈跟踪

stackTrace: java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask@5e2675f rejected from java.util.concurrent.ThreadPoolExecutor@f5f25ac[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 240]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2014)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:794)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1340)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:607)

导致异常的行

 new SetDownloadStatusTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

这里真的有两部分问题..

  1. 我想要一些帮助来理解异常的第一行:

    ThreadPoolExecutor@f5f25ac[运行,池大小 = 9,活动线程 = 9,排队任务 = 128,完成任务 = 240]

    池大小与活动线程与四任务..我假设我执行了太多任务,但我不确定如何确定这三个变量的真实情况。如果有人能为我分解它,我将不胜感激。

  2. 如果这是我 运行 线程不足的情况,我应该增加允许的数量吗?如果是的话,如何增加,或者是否有更好的解决方案。

自定义异步任务

private class SetDownloadStatusTask extends AsyncTask<Void, Void, DownloadStatus> {

        @Override
        protected DownloadStatus doInBackground(Void... params) {
            return bookDownloadManager.status(product);
        }


        @Override
        public void onPostExecute(DownloadStatus downloadStatus) {
            updateMenuForDownloadStatus(downloadStatus);
        }
    }

I assume I am executing too many tasks

正确。

If someone could break that down for me, Id appreciate it

AsyncTask.THREAD_POOL_EXECUTOR,在 quad-core CPU 上,将支持九个并行线程 (pool size = 9)。这是由最大长度 128 的 LinkedBlockingQueue 支持的。并且,您已经请求了第 138 个并发任务,因此我们没有线程 (active threads = 9) 并且队列已满 (queued tasks = 128) .

should I increase the amount allowed and if so how, or is there a better solution

你应该问问自己"why in the name of all that is holy am I trying to download 138 things at once?"。

如果有合法需要,请使用您自己的习惯 Executor,而不是 THREAD_POOL_EXECUTOR。如果并发下载 138 次以上没有合法需求——坦率地说,这个数字看起来很疯狂——那么请更改您的代码以避免这样做,例如取消您不再需要的任务。