Ankur Samarya
Ankur Samarya

Reputation: 229

Multiple write from multiple process in Android Content provider

I know Android's ContentProvider is known for multiple write from multi-process/multi-thread without throwing lock exception. In multithreaded environment it might have synchronized the method using read-write lock. But what about multi process where multiple object will be created. Can any one let me internal working of ContenProvider.

Upvotes: 3

Views: 977

Answers (1)

DanyBoricua
DanyBoricua

Reputation: 400

A ContentProvider is a manifest declared component, it is instantiated by the OS and is tied to the main process (unless the attribute process is specified differently). Therefore only a single instance of a declared ContentProvider is created during the life time of that application process. Other processes that wish to interact with it must go through the ContentResolver which in turn just communicate with that single provider created.

Note that when a provider is used from another process it is communicating via IPC (via Binders specifically), which means a provider's methods will be invoked in a BinderThread during an IPC call. There is a pool of binder threads so that there can be some concurrent communication with multiple application, meaning appropriate synchronization should be done.

In summary, content providers are safe for multi-process & multi-thread interactions because:

  1. They are a Singleton.
  2. Multi-thread safety is guarantee by the developer with appropriate synchronization of shared state.
  3. Multi-process safety is guarantee by the ContentProvider.
  4. The ContentProvider's definition has return types that are suitable for IPC interaction. (They are Parcelable, like AssetFileDescriptor, or they are a SQLiteCursor which is backed by allocated shared memory using ashmem).

Upvotes: 2

Related Questions