Suppose that an Android system service (ActivityManagerService for example) receives two requests at the same time. Will the system service process these requests one after the another, or will it process both at the same time?
I've seen some code in AOSP that intentionally performs asynchronous operations using handlers or threads (usually while writing settings to storage) Still, it's not clear to me what the default behavior is, e.g., for a simple system service method that doesn't have any specialized multi-threading code.
The actual question at hand is probably: if a system service receives transaction B while it's already processing transaction A, will it instantly start processing transaction B or wait until it's done processing transaction A?
I'm not sure how to test this without compiling AOSP and adding log statements, and compiling AOSP is not possible for me right now.
CodePudding user response:
Android use binder to do inter-process communication.
Every process will kepp a thread pool to react to binder transact, the binder thread's name is like binder:<pid>_x
.
If a system service (i.e ActivityManagerService
) raceive two binder transacts at the same time,
the system_server (which process system services live in) will pick up two threads from binder thread pool to react to the two binder transacts.
The two binder transacts can running parallelly with the two threads.
If the two binder transacts will change the same data, they should synchronized held a lock (i.e synchronized(ActivityManagerService.this)
),
the one get the lock first go ahead, the other will wait.
Another approach is when receiving two transacts with two binder threads, encapsulation and repost the transacts to a particular thread (i.e. android.bg) with handler. Then the android.bg thread will process the transacts synchronously.