This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Android and the fly likes How the handler object get the reference of UI thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "How the handler object get the reference of UI thread?" Watch "How the handler object get the reference of UI thread?" New topic
Author

How the handler object get the reference of UI thread?

Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Hi All,

I am just beginner in android. I came to know that using "android.os.Handler" we can run the GUI related code in GUI thread from non-GUI thread.

I am wondering how the handler object hold the reference of the GUI thread. Because when we create the handler object we are not passing any reference to the GUI thread.

Please clarify me, how it get the reference on this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Not sure about the details (you can look up those in the source code of the Handler class), but obviously somehow from the Android OS itself.

If you want to implement background processing that needs to update the UI, I advise to use AsyncTask instead, though: http://android-developers.blogspot.de/2009/05/painless-threading.html


Ping & DNS - my free Android networking tools app
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The Handler doesn't have any special code for working with the GUI thread - rather it makes sure all messages are delivered to the Thread in which the Handler was created. This helps you deliver messages to the GUI thread only when you create the Handler in the GUI thread (then access it from other threads.) If you started the Handler in some other thread that was not the GUI thread you could not send messages to the GUI thread.

Knowing this, it is just a matter of using Thread.currentThread() during the initialization process to get a reference to the Thread in which the Handler was created.


Steve
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Hi Luke,

Thanks for your reply.

Is the Handler follows event queue mechanism ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Sort of. It uses a MessageQueue. Essentially the EventQueue is just a MessageQueue associated with the GUI. For a Handler created in the GUI thread you would consider it as working with the EventQueue. But the Handler works with the current Thread's Looper/MessageQueue and not all threads initially have a Looper or MessageQueue, so if you use Handler (or call Looper.prepare()) then a MessageQueue will be created for the current thread if it does not already exist. This could mean that you have multiple different threads running event-like messaging loops.
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Thanks Steve & Dittmer.
 
Don't get me started about those stupid light bulbs.
 
subject: How the handler object get the reference of UI thread?