我的 Android 应用程序必须处理到达的消息,这些消息经常成束出现(尤其是在不稳定的连接期间)。我在 AsyncTasks 中处理这些传入消息,这样我就不会干扰 UI 线程。如果一次收到太多消息,我会收到一个RejectedExecutionException
. 我的错误堆栈如下所示:
10-22 14:44:49.398: E/AndroidRuntime(17834): Caused by: java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@414cbe68 rejected from java.util.concurrent.ThreadPoolExecutor@412716b8[Running, pool size = 128, active threads = 22, queued tasks = 0, completed tasks = 1323]
10-22 14:44:49.398: E/AndroidRuntime(17834): at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
10-22 14:44:49.398: E/AndroidRuntime(17834): at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
10-22 14:44:49.398: E/AndroidRuntime(17834): at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
10-22 14:44:49.398: E/AndroidRuntime(17834): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:564)
我正在运行任务,task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
以便并行处理传入的消息。
令人困惑的是,与我可以找到的相关 StackOverflow 问题(例如这里和这里)不同的是,活动线程和排队任务的数量似乎并没有超出限制(似乎是 128和 10,分别)。查看堆栈跟踪:
ThreadPoolExecutor@412716b8[Running, pool size = 128, active threads = 22, queued tasks = 0, completed tasks = 1323]
为什么我会收到此错误消息?