Whether a bound service or customized threads when download something?
Asked Answered
S

1

6

I'm working on an Android project, in which I need to load some images to a GridView. The images could be in the cache, if cache missing, then query the Internet server. My design is to use two threads to do the loading task. One thread for reading cache and one thread for downloading. So there are 3 threads including the UI one. Each of them has its own message queue and uses Handler to communicate.

My question is whether I should use bound service in this situation instead? Actually I have realized the design above and it seems nothing wrong but GC is working very hard, which can be inferred from the logcat.

Another issue is that several threads exist when I use DDMS to monitor the threads. This is because the same loading mechanism is used in several Activities. I have let the threads quit its message loop while onPause() is called, I'm sure only two of them are alive in the same time. But I can see all of them in DDMS. (BTW, why the threads still exist? I have let thread = null;)

So in a word, my question is: Could this loading task benefit from a bound service?

Soll answered 2/12, 2012 at 22:12 Comment(0)
P
1

So why are you using multiple threads? You need to load an image in the background, and when done, display it in the UI. It really doesn't matter to the UI where the images comes from. And using multiple threads doesn't make it faster, just consumes more memory. Just use a single background thread: first it hits the cache, then downloads if no hit.

Services are for when you want do something that doesn't need a UI. If you need to update the UI in real time, a service doesn't make much sense.

Periwig answered 3/12, 2012 at 1:32 Comment(10)
Thanks for your reply. But one background thread for caching and downloading is not enough. In that way, an image in the cache won't be loaded until the previous downloading tasks finish. In the two-background-thread design, an image could be loaded as soon as possible if it's in the cache. As far as I know, 'Bound Service' can be used to communicate with the activity. This communication may be bilateral, so theoretically a bound service applies to this situation. But what I'm wondering is whether the bound service is more efficient.Soll
A service is in no way special: if you want it to run threads, you have to create them. For your case, you might want to have multiple (but bound) loading threads so that you get things faster. There are open source libraries that do this, and IIRC there are some examples in AOSP code. In any case, a service is not thing to use here.Periwig
Here's a very good one: github.com/novoda/ImageLoader. If you check the source you will see that this is not trivial, and you might want to use a library instead of trying to reinvent it.Periwig
Thx for your recommendation. I've tried this library today. It's a nice utility, however, not what I need. For several minor issues. My server will block one user with too much concurrent queries. So that I need to disable the multi-thread function provided by this library, in which situation this lib becomes the single thread as your first design. Another important issue is that this lib makes highly use of AsyncTask, which I'm using to do other things. They will block each other due to the system thread pool. Besides, the order of the downloading tasks is out of my control.Soll
Your recommendation is really useful for me. I have moved my ImageLoader instance into the Application class, just like what they did in the open source one. There aren't too many threads in DDMS now.Soll
In any case use a thread pool to keep an upper bound on the number of threads. Also much faster than creating threads each time.Periwig
By moving the downloader to the application class, there is no need to create threads each time now. I call removeMessages() method when an activity's onPause() called. But the threads are still in their loop, with empty message queues. No issue found so far.Soll
As long as you are not creating more threads as the number of items in your list(s) increase, you should be OK. Still better to use an explicit pool if you are not using one. Also consider accepting the answer if it was helpful :)Periwig
Thanks for your help. Even though I intended to learn something about the inner mechanism of bound service... Anyway, you helped me a lot, Thank you.Soll
You can always ask a new question. More focused is usually better.Periwig

© 2022 - 2024 — McMap. All rights reserved.