The setup: I have service class and two activities that bind to it. The first is the main UI activity that calls on
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE).
The second activity only calls on bindService(intent, mConnection, Context.BIND_AUTO_CREATE).
Both calls unbindService(mConnection) in onStop().
There's a button call "button A" on the main UI that when press will pass an arrayList to a method on the service. The method in the service then starts a new static thread to process this arrayList. The new thread also has a static message handler to communicate with the methods in the service. The second activity calls on the service to perform a calculation and then saves the result in a database.
I only explicitly declared one thread in the service. So I expect 2 threads total including the main thread and I only expect two binders since I only have two activities that bind to the service. However, when I do a Thread.activeCount() in onCreate of main UI, I initially get 3 threads. Later I get 4 threads, followed by 9 threads and 11 threads as I proceed in the manner described below.
Here's the questions.
How are the binders and asynctasks added to my thread group's list? Why do they keep expanding? I am concerned about battery consumption, will more threads mean more battery consumption? Or it just does the work faster and these automatically generated threads will garbage collect themselves? Do I have any memory leaks? Can I control how these threads are spawned?
I didn't find much documentation on this. Anyone understand this matter please let me know.
First run of the app, in onCreate of main UI:
ThreadGroup ctg = Thread.currentThread().getThreadGroup(); ctg.list();
The list shows:
I/System.out(18606): Thread[main,5,main]
I/System.out(18606): Thread[Thread-5,5,main]
I/System.out(18606): Thread[Binder_1,5,main]
I/System.out(18606): Thread[Binder_2,5,main]
I know main, Binder_1, Binder_2 are the 3 active threads. I reorient my phone, logcat shows:
MainActivity onStop unbindService
MainActivity onDestroy() plus debuginfo --Pid is 18606, threadid is 18606, there are 4 threads
MainActivity onCreate() plus debuginfo --Pid is 18606, threadid is 18606, there are 4 threads
I/System.out(18606): java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606): Thread[main,5,main]
I/System.out(18606): Thread[Thread-5,5,main]
I/System.out(18606): Thread[Binder_1,5,main]
I/System.out(18606): Thread[Binder_2,5,main]
I/System.out(18606): Thread[Binder_3,5,main]
Service onStartCommand() 2--Pid is 18606, threadid is 18606, there are 4 threads
Then I press "button A" on main UI to process arraylist by creating a new thread, then I get 11 threads. Logcat shows:
D/Service(18606): in processRuleOnArrayList -- ACTUALLY MADE A NEW THREAD
D/BoundService(18606): processRuleOnArrayList(): --Pid is 18606, process threadid is 18606, service threadid is 2930, service threadname is serviceName_0, there are 11 threads
D/Service(18606): processRuleOnArrayList(): service thread's threadGroup main, main thread's threadGroup java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606): java.lang.ThreadGroup[name=main,maxPriority=10]
I/System.out(18606): Thread[main,5,main]
I/System.out(18606): Thread[Thread-5,5,main]
I/System.out(18606): Thread[Binder_1,5,main]
I/System.out(18606): Thread[Binder_2,5,main]
I/System.out(18606): Thread[Binder_3,5,main]
I/System.out(18606): Thread[AsyncTask #1,5,main]
I/System.out(18606): Thread[AsyncTask #2,5,main]
I/System.out(18606): Thread[AsyncTask #3,5,main]
I/System.out(18606): Thread[AsyncTask #4,5,main]
I/System.out(18606): Thread[AsyncTask #5,5,main]
I/System.out(18606): Thread[Binder_4,5,main]
I/System.out(18606): Thread[serviceName_0,5,main]