Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Difference between a thread's message queue and event queue

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Difference between a thread's message queue and event queue

    I was just seeing the documentation of three methods which can be used to execute a piece of code in the UI thread while we are working in a worker thread. The methods are:

    1. public final void runOnUIThread(Runnable action) - Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread

    2. public boolean post(Runnable action) - Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.

    3. public boolean postDelayed(Runnable action, long delayMillis) - Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface thread.


    The first one posts the Runnable to the event queue of the UI thread, while the other two add the Runnable to the message queue. Please tell me the difference between the two?

    My web search tell me that an event queue is simply a queue of events waiting to be executed by the thread. I am not clear about the message queue. MessageQueue is some class as well, is this related to that?

    Thank you in advance.

  2. #2
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Re: Difference between a thread's message queue and event queue

    Quote Originally Posted by Norm View Post
    Sorry, I moved it here. I missed the Android part of the post.
    So please move it back? So that I can possibly get some replies.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Difference between a thread's message queue and event queue

    Do you have some code that you are having problems with? Please post it.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Difference between a thread's message queue and event queue

    They're the same queue as far as I'm aware.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Re: Difference between a thread's message queue and event queue

    @Norm, I was reading up on Processes and Threads in Android, so I don't have a code. I just did not understand the description of these methods in the documentation.

    @Tolls, Thank you very much.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Difference between a thread's message queue and event queue

    And btw, if you post the same question to multiple forums, please post a link to your other questions so people may follow it and see what has already been answered:

    android - Difference between event queue and message queue - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Difference between a thread's message queue and event queue

    IMO, Loopers and Handlers are one of the coolest things about the Android API. There's nothing you can do with them that you can't do some other way, but they make a lot of things simpler.

    In AWT/Swing, there's a UI thread (which is different from the JVM's main thread) that runs in a loop, processing tasks as they are added to its queue and sitting idle when there's nothing to do. A Looper thread is like that. The main thread on Android is a Looper. Before it enters your code, it has already called Looper.prepare() and Looper.loop(). The really cool thing is that by calling these methods, any background thread can have its own task queue.

    A Handler is just a convenient entry point into a Looper's event queue. Whatever Looper thread the Handler was created in is the thread whose event queue the Handler points to. Mostly you'll post tasks from the UI thread to the UI thread. But the post methods are thread-safe, so it's easy to pass tasks to background Loopers if you ever need to. The really cool thing about Handlers is that they are also part of Android's IPC framework. You can construct a Messenger from a Handler, and then obtain a Binder from the Messenger that you can pass to another process, like by returning it from a Service's onBind method.

    Sending a Message to a handler is a little different from posting a Runnable. Not only will the Message be handled in the Handler's Looper thread, but it will be handled by the handleMessage method of the specific Handler it was sent to. The main reason you would extend Handler is to override that method.
    Zarah likes this.
    Get in the habit of using standard Java naming conventions!

  8. #8
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Re: Difference between a thread's message queue and event queue

    @Kjkrum, Thank you for explaining it, it had clarified my concept.

    The really cool thing about Handlers is that they are also part of Android's IPC framework.
    Is an AsyncTask also a part of the IPC framework?

    Moreover I am new to learning it all. And I was thinking that when there are more than one ways of doing a thing, the idea in my mind is that it's OK to learn just one way and you're good to go. So I am learning and practicing using AsyncTask, so will there be times when I can't do a job with AsyncTask and I'll particularly need handlers? And secondly is using one class more beneficial than the other (in terms of efficiency (time taken in execution) of program execution or something)?

    I'll be thankful if you can suggest it.

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Difference between a thread's message queue and event queue

    No, an AsyncTask is just a convenient way to do something in a background thread in the same process. AsyncTasks and Handlers are very different.
    Get in the habit of using standard Java naming conventions!

  10. #10
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Re: Difference between a thread's message queue and event queue

    @kjkrum, Thank you very much.

Similar Threads

  1. Event Queue
    By netboy in forum New To Java
    Replies: 3
    Last Post: 11-22-2011, 01:38 PM
  2. Replies: 0
    Last Post: 05-27-2011, 09:46 AM
  3. Single thread to handle queue based logging
    By impacted in forum New To Java
    Replies: 4
    Last Post: 04-28-2011, 11:25 PM
  4. event queue
    By nikkka in forum New To Java
    Replies: 2
    Last Post: 04-22-2011, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •