如何正确处理AsyncTasks的RejectedExecutionException?

时间:2014-05-06 16:09:21

标签: android android-asynctask

我目前有BaseAdapter填充缩略图库。当项目可见时,从适配器AsyncTask方法启动getView()以下载缩略图,完成后,使用正确的位图更新imageview。这似乎工作得非常好,除非用户滚动得非常快。这是因为在任何给定时间在队列中保留128个线程的AsyncTask限制。

所以我的问题是如何在安排其他任务之前正确检测线程池是否已满?或者我如何正确捕捉错误,以便我没有收到强制关闭?

以下是我收到的错误:

5-06 10:54:11.416  27931-27931/com.diverg.tidy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.diverg.tidy, PID: 27931
java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@426c4e80 rejected from java.util.concurrent.ThreadPoolExecutor@41bddce0[Running, pool size = 5, active threads = 5, queued tasks = 128, completed tasks = 35]
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
        at   java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:590)
        at com.diverg.tidy.MediaAdapter.getView(MediaAdapter.java:90)
        at android.widget.AbsListView.obtainView(AbsListView.java:2263)
        at android.widget.GridView.makeAndAddView(GridView.java:1345)
        at android.widget.GridView.makeRow(GridView.java:345)
        at android.widget.GridView.fillDown(GridView.java:287)
        at android.widget.GridView.fillFromTop(GridView.java:421)
        at android.widget.GridView.layoutChildren(GridView.java:1233)
        at android.widget.AbsListView.onLayout(AbsListView.java:2091)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.ListView.setupChild(ListView.java:1882)
        at android.widget.ListView.makeAndAddView(ListView.java:1793)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillGap(ListView.java:655)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4247)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at   android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 05-06 10:54:13.299  29540-29540/? E/dalvikvm﹕ /system/priv-app/GooglePartnerSetup.apk odex has stale dependencies
05-06 10:54:19.346    8024-8024/? E/fb4a(:<default>):AddressResolver﹕ Failed to deserialize to instance com.facebook.mqtt.AddressEntry
        at [Source: java.io.StringReader@42180f18; line: 1, column: 34]
05-06 10:54:20.018      125-439/? E/Netd﹕ Failed to write /sys/class/net/rmnet1/mtu: Invalid argument
05-06 10:54:20.025      447-522/? E/ConnectivityService﹕ exception in setMtu()java.lang.IllegalStateException: command '5905 interface setmtu rmnet1 1500' failed with '400 5905 Failed to get MTU (Invalid argument)'
05-06 10:54:20.369     447-1037/? E/ConnectivityService﹕ startUsingNetworkFeature took too long: 330ms
05-06 10:54:20.393      125-439/? E/Netd﹕ Failed to write /sys/class/net/rmnet1/mtu: Invalid argument
05-06 10:54:20.393      447-522/? E/ConnectivityService﹕ exception in setMtu()java.lang.IllegalStateException: command '5917 interface setmtu rmnet1 1500' failed with '400 5917 Failed to get MTU (Invalid argument)'
05-06 10:54:21.439      447-503/? E/WifiStateMachine﹕ Unexpected BatchedScanResults :OK
05-06 10:54:22.096      447-522/? E/ConnectivityService﹕ Unexpected mtu value: android.net.wifi.WifiStateTracker@423a09c0
05-06 10:54:23.932  29936-29936/? E/dalvikvm﹕ /system/priv-app/SetupWizard.apk odex has stale dependencies
05-06 10:54:24.127  29936-29936/? E/PhoneMonitor﹕ onOtaspChanged old =0, new =3
05-06 10:54:24.338  29957-29957/? E/dalvikvm﹕ /system/app/GalleryGoogle.apk odex has stale dependencies
05-06 10:54:25.127      125-439/? E/SecondaryTablController﹕ ip route del failed: /system/bin/ip route del 10.182.14.1/32 via 0.0.0.0 dev rmnet1 table 60
05-06 10:54:25.143      125-439/? E/SecondaryTablController﹕ ip route del failed: /system/bin/ip route del 0.0.0.0/0 via 10.182.14.1 dev rmnet1 table 60
05-06 10:54:25.150      125-439/? E/SecondaryTablController﹕ ip route del failed: /system/bin/ip route del 2600:1014:b10b:6554::1/128 via :: dev rmnet1 table 60
  05-06 10:54:25.166      125-439/? E/SecondaryTablController﹕ ip route del failed: /system/bin/ip route del ::/0 via 2600:1014:b10b:6554::1 dev rmnet1 table 60

2 个答案:

答案 0 :(得分:0)

您可以浏览 AsyncTask 的文档: http://developer.android.com/reference/android/os/AsyncTask.html

您可以使用As getStatus()的AsyncTask来了解工作线程的状态。见http://developer.android.com/reference/android/os/AsyncTask.html#getStatus%28%29

答案 1 :(得分:0)

我最终使用this作为模板。它的工作原理是创建一个工作线程并将图像添加到它的队列中。