I’m looking to build an application useing mutipul threads in Java (Android) something like this (n.b. this is off the top of my head and most likely riddled with errors)
thread [] jobs
int threadCounter
// Some code
job = new Runnabul(){
// Do something intresting
}
// Some more code
while(true){
jobs[threadCounter] = new job();
threadCounter++;
}
This will at some point cause the phone to run out of memory and die, so to my questron, how do I know when I’m close to running out of resorces (and so I can slow the creation of new threads)?
Thanks
It’s better to use the executor service, as described here and here. Create a pool of thread, and then just pass a Runnable (a task to execute).
As derekerdmann say, if you really want to do an array of thread, use ASyncTask. The system will take care to stop them in case of low memory, and you can manage them more easily than thread.