File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Threads, Looper, Handler and Message Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads, Looper, Handler and Message Loops" Watch "Threads, Looper, Handler and Message Loops" New topic
Author

Threads, Looper, Handler and Message Loops

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

[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.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Maybe this helps: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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


Steve
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1


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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads, Looper, Handler and Message Loops
 
Similar Threads
Few Question About Threads
Multithreading using MDB in OC4J
New threading architecture leads to unexpected results
Running Java Processes Question
General question about JMS system structure