I know that for keeping a responsive interface in Android the heavy work must be completed in an independent thread. I understand well how to accomplish this (by using AsynTask..., etc), and this is not the point of the question, just for everybody to know.
But I've been struggling for a while with a very simple parallel program. This program search for the lowest integer in an array witch length is 15000000.
I implemented this runnable:
...
run(){
highestInteger = integers[firstIndex];
for(int i = firstIndex; i < secondIndex; i++){
if(highestInteger<integers[i]){
highestInteger = integers[i];
}
}
}
... so I could look for the highest integer in the first half of the array (in one thread) and look for the highest integer in the other half of the array (in the second thread).
The program works very well on my computer (as a java/not-android program) and by very well I mean that the parallel times are shorter (almost by a half) than the serial ones.
But on my android tablet (4 cores) the times are often the same and the serial ones are almost always shorter.
I do have notice (with the debugger) that in my tablet there are several threads running:
So there are 3 threads running, and I need at least 2 free cores for my program to run efficiently. I've read a bit about binder threads but I don't really understand that very well.
Is there a way to solve this or not? Is there a way in which I can avoid the automatic creation of those binder threads or not? Or it is not possible to get this kind of threading to work until we have like a 6 core device?
I do have notice (with the debugger) that in my tablet there are several threads running
You have several threads that have been created. Most will be blocked waiting on I/O.
Is there a way to solve this or not?
The decision of core allocation is made by the operating system and will take into account other programs, plus power consumption (keeping all four cores running at all times will be really bad for the battery), as Andy Fadden (of the core Android team) points out in this SO comment and this SO comment. Note that there are ~750 million Android devices in use today, the vast majority of which will have fewer than four cores, and most of those with only a single core, and so you need to take that into account as well.
Is there a way in which I can avoid the automatic creation of those binder threads or not?
Only by not writing an Android app. Those threads are used for inter-process communication, which is essential to running an Android app.
Or it is not possible to get this kind of threading to work until we have like a 6 core device?
It is certainly possible. Andy Fadden demonstrates it in this StackOverflow answer. There may be ways to reorganize your algorithm to make better use of SMP on Android, as is outlined in the documentation. You might also consider Renderscript Compute as an alternative to doing your work in Java.