Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:24:22+00:00 2026-06-02T22:24:22+00:00

When I use Handler.dispatchMessage(msg) , the handleMessage(Message msg) will be run on new thread

  • 0

When I use Handler.dispatchMessage(msg), the handleMessage(Message msg) will be run on new thread but when I use Handler.sendMessage(msg), the handleMessage(Message msg) will be run on main thread. Who can tell me the difference between them?

Thanks!

Demo:

public class MainActivity extends Activity
{
private String TAG = "MainActivity";
private Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        Log.i(TAG, "Handler:" + Thread.currentThread().getId() + " & arg1=" + msg.arg1);
        super.handleMessage(msg);
    }

};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, "Main:" + Thread.currentThread().getId());
    testMethod();
}

private void testMethod()
{
    Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            Log.i(TAG, "Thread:" + Thread.currentThread().getId());
            Message msg = mHandler.obtainMessage();
            msg.arg1 = 1;
            mHandler.dispatchMessage(msg);
            Message msg2 = mHandler.obtainMessage();
            msg2.arg1 = 2;
            mHandler.sendMessage(msg2);
        }

    };
    thread.start();
}

}

Output:

04-19 11:32:10.452: INFO/MainActivity(774): Main:1
04-19 11:32:10.488: INFO/MainActivity(774): Thread:8
04-19 11:32:10.492: INFO/MainActivity(774): Handler:8 & arg1=1
04-19 11:32:10.635: INFO/MainActivity(774): Handler:1 & arg1=2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:24:26+00:00Added an answer on June 2, 2026 at 10:24 pm

    mHandler.dispatchMessage(msg) is like directly calling handleMessage(Message msg) and I don’t know when that would be useful. The point of Handlers is the ability to send messages to other threads. That’s what you do with sendMessage.

    Edit: as you can see it just calls handleMessage() for you.

    /**
     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            // callback = the Runnable you can post "instead of" Messages.
            msg.callback.run();
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use output caching with a .ashx handler? In this case I'm
I am trying to use logging's memory handler to buffer log messages to my
I use logging settings as below in the settings.py file: logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT); handler =
In my APP. Need to use Lazy Loading, so I have to handler the
I'm pretty new to Java ME and i'm trying to use Microlog to handle
When do you need to use this type of modified message loop in multithreaded
For deleting the app data i am trying to use reflection but getting NPE
How can I extend ImageView , so that I can use in my code
We often use event handler in C#, just as below: some_event+=some_event_handler; Does this happen
I'm looking to avoid having to use a handler/module in my Webrole to protect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.