简体   繁体   中英

Are Android system services asynchronous by default?

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, eg, 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.

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 (ie 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 (ie 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 (ie android.bg) with handler. Then the android.bg thread will process the transacts synchronously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM