Android: What is Binder Thread?

后端 未结 3 1716
孤城傲影
孤城傲影 2020-12-24 08:27

I use Debug.startMethodTracing for my purposes and in the output file I can see(I don\'t use IPC):

8   Binder Thread #2
7   Binder Thread #1
<
相关标签:
3条回答
  • 2020-12-24 08:45

    Binder thread represents a separate thread of your service. Binder is a mechanism that provides Inter Process Communication.

    Let's consider an example. Imagine that you have service Process B (see picture). And you have several applications that communicate with this service B (one of this application is, for instance, Process A). Thus, one service B should provide different results simultaneously to different applications. Thus, you need to run several replicas of Service B for different applications. Android runs these replicas in different threads of the Process B and these threads are called "Binder Thread #N".

    Binder communication

    I took the picture here, where you can also read what Binder is.

    0 讨论(0)
  • 2020-12-24 08:50

    Binder Thread is used in Android Service Binding with Interprocess Communication. Most of the time you will encounter this concept in Service calls with interfaces defined by Android Interface Definition Language (AIDL).

    In the AIDL case, Service calls are executed by threads maintained by a default Thread Pool created with your application. Those threads are called Binder Threads. This grants the Service the ability to work on multiple calls happening at the same time.

    Usually, Service calls with the interface defined by "Extending the Binder class" and "Using a Messenger" are executed sequentially in one thread.

    A detailed discussion about "Service Binding and Threads" can be found here.

    In short:

    Calls made from the local process are executed in the same thread that is making the call.

    Calls from a remote process are dispatched from a thread pool the platform maintains inside of your own process.

    You must be prepared for incoming calls from unknown threads, with multiple calls happening at the same time. In other words, an implementation of an AIDL interface must be completely thread-safe.

    0 讨论(0)
  • 2020-12-24 08:53

    "Binder is a mechanism that provides Inter Process Communication."

    Binder isn't necessarily just an IPC mechanism.

    All cross component traffic is abstracted from Binder, a local intent for example is a binder abstraction.

    0 讨论(0)
提交回复
热议问题