Вопрос

I currently have a BaseAdapter that is populating a gallery of thumbnails. When an item is visible, an AsyncTask is started from the adapters getView() method to download the thumbnail and when completed the imageview is updated with the correct bitmap. This seems to work really well, except when the user scrolls really fast. This is because of the AsyncTask limitations of holding 128 threads in the queue at any given time.

So my question is how do I properly detect whether the thread pool is full before scheduling another task? Or how do I properly catch the error so I don't receive a force close?

Here are the errors I'm receiving:

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

I ended up using this as a template. It works by creating a worker thread and adding images to it's queue.

Другие советы

You can go through documentation of AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

And you can use getStatus() of AsyncTask to know the status of the worker thread. See http://developer.android.com/reference/android/os/AsyncTask.html#getStatus%28%29

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top