Lee Yue
Lee Yue

Reputation: 67

Android inter-thread communication

I read from a book,

"The most common thread communication use case in Android is between the UI thread and worker threads. Hence, the Android platform defines its own message passing mechanism for communication between threads. The UI thread can offload long tasks by sending data messages to be processed on background threads. The message passing mechanism is a nonblocking consumer-producer pattern, where neither the producer thread nor the consumer thread will block during the message handoff."

How could I comprehend the "nonblocking"? In my opinion, if the messge queue has no message to send to consumer thread(maybe UI thread), then consumer thread has to wait until there is one message to get.

Upvotes: 2

Views: 286

Answers (2)

VikasGoyal
VikasGoyal

Reputation: 3376

Let's First understand what is this and their work.

Main Thread(UI Thread):- In Android application MainThread or UI thread should be always used for only UI operation. UI operation are like(UI rendering, UI updating, UI event listening, User action on UI like scrolling, UI animation etc.)

If Your Main thread is doing any blocking operation like (Inserting data in DB, reading data from DB or any other blocking operation which take lots of time in completion take an example 20 seconds) and

Case 1:- At the same time User try to perform some action on UI like click on button or try to scroll list but you main thread is not able to respond to the user action as he is busy with your other operation which is not completed. So in such case android wait for 5 seconds and if main thread still not responds then show ANR(Android not responding dialogue).

Case 2:- Even if User doesn't perform any action at the same time and you keep your main thread busy for more than 10 seconds Android show the same ANR dialogue.

Background Thread:- Background thread or worker thread are started by android application to do some heavy CPU intensive work to keep our main thread free for UI related work.

Now come to your question, you have doubt about the main thread is in wait condition to get a new message or any event, so how it is 'nonblocking' when it is in 'Waiting' state? Wait state doesn't mean he is blocked for a particular event in wait state main thread is actually free and at this time any one can call him and assign work. So waiting state is not a blocking state it a nonblocking state.

Upvotes: 2

Bqin1
Bqin1

Reputation: 507

Quote "How could I comprehend the "nonblocking"? In my opinion, if the messge queue has no message to send to consumer thread(maybe UI thread), then consumer thread has to wait until there is one message to get."

Your misunderstanding here is that you assume the consumer thread sole purpose is to wait for producer thread. In which case it would be "blocking"

however realize that the consumer could be a service doing its job, and activity can talk to it at any time to change its job. Thus the service is never blocked by the activity, it's simply able to take messages and keep going.

Upvotes: 1

Related Questions