简体   繁体   中英

Android - should I use IntentService or ThreadPool in my program?

My program will be searching file system, so I know I need Service or IntentService, but I am not sure which one. (Formerly I wanted to use AsyncTask but that is not suitable)

I know that IntentService will run one thread at a time, and actually this might be what I want but not sure

If there is a removable SD Card in phone, I will perform searching on separate threads. One thread will search Internal SD and another External SD.

Problem is that, I can input multiple paths to folders in which files will be searched for, but I will use only one thread for one filesystem. For example I have 10 paths to search on Internal SD and 5 on External but I will use only two threads. One searching Internal, one external, and when any of threads is done it will launch another search on Internal and same can happen when searching External.

I don't know if it would be better to use Service with multiple threads, or two IntentServices, if that is even possible?

I would use a Service (working similar to IntentService but not a subclass) that can distribute tasks to a threadpool.

IntentService can only do 1 background thread. If that's enough for you (It's probably performing close to a multithreaded implementation) you could do that as well. Just enqueue each directory / filesystem / task as an intent to handle for the service.

It should be a Service since Android will kill your app when it does not know about running tasks. A plain thread is not considered running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM