RejectedExecutionException从AsyncTask的,但都没有击中限制 [英] RejectedExecutionException from AsyncTask, but haven't hit limits

查看:994
本文介绍了RejectedExecutionException从AsyncTask的,但都没有击中限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序必须处理到达频繁进来束(尤其是在片状连接段)的消息。我在处理这些AsyncTasks传入的消息,让我不与UI线程干扰。如果有太多的消息一下子进来,我收到了 RejectedExecutionException 。我的错误堆栈看起来是这样的:

  14 10-22:44:49.398:E / AndroidRuntime(17834):java.util.concurrent.RejectedExecutionException:所致任务android.os.AsyncTask$3@414cbe68从拒绝java.util.concurrent.ThreadPoolExecutor@412716b8 [跑步,池大小= 128,活动的线程= 22,排队任务= 0,已完成的任务= 1323]
10-22 14:44:49.398:E / AndroidRuntime(17834):在java.util.concurrent.ThreadPoolExecutor中的$ AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
10-22 14:44:49.398:E / AndroidRuntime(17834):在java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
10-22 14:44:49.398:E / AndroidRuntime(17834):在java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
10-22 14:44:49.398:E / AndroidRuntime(17834):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:564)

我正在与 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR),这样收到的消息是并行处理的。

任务

什么是关联计算器问题,这个混乱的,不同的是我能找到的(例如<一个href=\"http://stackoverflow.com/questions/2492909/asynctask-rejectedexecutionexception-and-task-limit\">here和<一个href=\"http://stackoverflow.com/questions/17138095/rejectedexecutionexception-128-active-threads-asynctaskloader\">here),是,似乎活动线程和排队任务的数目不被碰撞靠在限制(这似乎是128和10,分别)看栈跟踪:

的ThreadPoolExecutor @ 412716b8 [跑步,池大小= 128,活动的线程= 22,排队任务= 0,已完成的任务= 1323]

为什么我会收到此错误信息?


解决方案

  

为什么我会收到此错误信息?


如果您已经超出了可以由的ThreadPoolExecutor 排队的任务数你会得到此错误消息。唯一一次 RejectedExecutionException 被抛出是由 ThreadPoolExecutor.AbortPolicy 这是默认的 RejectedExecutionHandler

要在<报价href=\"http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html\">javadocs:


  

如果请求不能被排队,则创建一个新的线程,除非这会超出maximumPoolSize,在这种情况下,任务将被拒绝。


有是任务的一个最大数目。看到这个问题/答案在这里:<一href=\"http://stackoverflow.com/questions/9593850/is-there-a-limit-of-asynctasks-to-be-executed-at-the-same-time\">Is有AsyncTasks的限制的同时被执行?

下面是一个链接的源$ C ​​$ C的<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/1.6_r2/android/os/AsyncTask.java#AsyncTask\">AsyncTask.

 私有静态最终诠释CORE_POOL_SIZE = 5;
私有静态最终诠释MAXIMUM_POOL_SIZE = 128;私有静态最后的BlockingQueue&LT;&的Runnable GT; sWorkQueue =
        新的LinkedBlockingQueue&所述;可运行&GT;(10);私有静态最后的ThreadPoolExecutor sExecutor =
    新的ThreadPoolExecutor(CORE_POOL_SIZE,
        MAXIMUM_POOL_SIZE,KEEP_ALIVE,TimeUnit.SECONDS,sWorkQueue,sThreadFactory);

所以它看起来像它开始于5个线程,拥有10队列一旦队列已满,可以启动128个线程。所以看起来你已经超过138并发请求。

My Android app has to deal with arriving messages which frequently come in bunches (especially during periods of flaky connectivity). I handle these incoming messages in AsyncTasks so that I don't interfere with the UI thread. If too many messages come in at once, I get a RejectedExecutionException. My error stack looks like this:

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)

I'm running the tasks with task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) so that incoming messages are processed in parallel.

What is confusing about this, and different from related StackOverflow questions that I can find (e.g. here and here), is that the number of active threads and queued tasks don't seem to be bumping up against the limits (which seem to be 128 and 10, respectively). See the stacktrace:

ThreadPoolExecutor@412716b8[Running, pool size = 128, active threads = 22, queued tasks = 0, completed tasks = 1323]

Why would I be getting this error message?

解决方案

Why would I be getting this error message?

You would get this error message if you have exceeded the number of tasks that can be queued by the ThreadPoolExecutor. The only time the RejectedExecutionException is thrown is by the ThreadPoolExecutor.AbortPolicy which is the default RejectedExecutionHandler.

To quote from the javadocs:

If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.

There is a maximum number of tasks. See this question/answer here: Is there a limit of AsyncTasks to be executed at the same time?

Here's a link for the sourcecode for AsyncTask.

private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;

private static final BlockingQueue<Runnable> sWorkQueue =
        new LinkedBlockingQueue<Runnable>(10);

private static final ThreadPoolExecutor sExecutor =
    new ThreadPoolExecutor(CORE_POOL_SIZE,
        MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);

So it looks like it starts at 5 threads, has a queue of 10. Once the queue is full it can start up to 128 threads. So it looks like you have exceeded 138 simultaneous requests.

这篇关于RejectedExecutionException从AsyncTask的,但都没有击中限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆