• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it a bad idea to use too many threads on Android?

 
Daniel Crawford
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you saw my previous post, I've been trying to find all the devices on a network. I've been able to do this using the system/bin/ping command. As you might assume, this is a very time consuming task for one thread. I haven't played around with threads much... so I would like someone's opinion on this.

Is it a bad idea to try having 255 threads running at once? Is there a possible way to query 255 threads but only run 20 of them at a time?

I've used Android's AsyncTask class before but it seems you can only have up to 128 threads queued at a time.

So I guess this means I'll need to use Runnable threads for my tasks.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,

I don't know how familiar you are with CPU configuration and threading, but on the new phones if you have a quad core that is dual threaded per core then you have 8 thread that can run simultaneously. That is 8 threads to everything the phone does--all android features, texting, memory management, Java, and any other apps that are running. You say it is limited to 128, but realistically it is limited functionally to much less for you to use than that. When you have 128 thread running those 8 hardware threads are getting time sliced like crazy and you are sharing those channels with all the other tasks on the device. The more you take, the more sluggish the phone or tablet is going to act when your app is running. Also you have to ask at what point do you find diminishing returns on the number of threads you want to use? Too many threads and you will not be able to handle them all effectively--no time savings and you may end up taking more time due to resource sharing overhead to service the number of threads you use. That is not to mention the resource battle that will go on between your app and the rest of the devices apps to get serviced.

So realistically the question comes down to how many thread is prudent to run for an android app? You have been running 1, I would try 2 and then 4, and then increase to 8 and see what happens for your time. Then stream some audio as you are doing this on wifi and 4G and see what kind of impact you have on the audio quality and what the streaming load does to your app.

It's going to be a tradeoff on CPU power and threading that is for sure, but I cannot give you any formula to work with--I don't work on CPU's that small--try the doubling approach and see how things test out: I will be quite surprised if you see any increase past 8 to 16 threads and at 16 I would also be surprised if you were not impacting all the other apps on the phone.

Les
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
256 threads which are all doing network IO won't load the CPU as much as 256 threads which are doing something CPU intensive, like say matrix multiplication. These IO threads will simply be in IO wait most of the time waiting for a ping reply (which will never come from majority of IP addresses).

However, you are still likely to see a CPU usage spike during this period because these are all java threads, not OS threads, which means the JVM has to do a bit of bookkeeping per thread, and also an increase in CPU context switches, which means the kernel will get busy for a while.

If this scanning is a one time or infrequent thing, then I wouldn't worry. isReachable or ping -c1 will complete very fast.
But if it's something done frequently every 10 seconds or something, then you don't want so many threads. As Les has already mentioned, it'll impact everything else.

Another problem is, 256 threads doing InetAddress.isReachable() is much easier on memory usage than launching 256 ping processes. Each process takes up a magnitude larger memory than a thread.
Is there a reason you switched to launching ping instead of using isReachable?

Don't use AsyncTask for this, because it runs all your tasks sequentially on 1 background thread by default.
Either use AsyncTask.executeOnExecutor() or use your own fixed threadpool using Executors.newFixedThreadPool, and notify the UI using a UI thread Handler (see this tutorial). As Les has explained, scale the fixed thread pool size gradually.

You can monitor the effects of different configurations on the device's CPU and memory by running vmstat via adb shell: ./adb shell vmstat -d 1.
The columns of interest would be:
free - how much memory is free
cs - how may context switches
us - how much CPU is spent in userland
sy - how much CPU is spent in kernel
id - how much CPU is spent idle
wa - how much CPU spent in IO wait
 
Daniel Crawford
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand much about threads, but your replies helped me understand them a bit better.

Thank you, guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!