• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads, Looper, Handler and Message Loops

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I would be vague in my description of the problem as my knowlege in this area is limited. Please excuse.]

I was reading up on Threads in Android and wanted to know about how inter-thread communication happens over message loops, in general.

In Android, when Looper.prepare() is called inside run method of any thread, a message queue is initialized for that thread. Other threads, can post messages to this queue. To handle these messages, a handler needs to be bound to the message queue. The Handler(class) will have callbacks which would be invoked whenever appropriate messages are posted in the thread's message queue.

Is this a standard way/mechanism for inter-thread communication?

Any links etc. that discuss this topic are appreciated.

Thanks.
 
Lester Burnham
Rancher
Posts: 1337
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monu Tripathi wrote:
Is this a standard way/mechanism for inter-thread communication?


Having a single thread which processes messages from a queue is a very standard approach to inter-thread communications. It basically is how the Swing's event handling works. The Android Looper/Handler paradigm makes it a little easier to make your own message processing thread, but isn't terribly different than, for example, a single threaded use of the ExecutorService API in Java 5+.


Any links etc. that discuss this topic are appreciated.


Someone asked about the Looper/Handler a little while ago. Here is my attempt at showing what the code might be like: Looper/Handler in Android
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the link Lester!

Steve Luke wrote:Someone asked about the Looper/Handler a little while ago. Here is my attempt at showing what the code might be like: Looper/Handler in Android

very cool!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic