0

We know main(UI) Thread is having implicit Looper attach with it so that its having MessageQueue.

And we also know that we can attach MessageQueue to worker thread with the help of Looper.prepare()

My question is that is it both Queue are same?

3
  • same? what do you mean? did you compare their references? Commented Dec 30, 2014 at 12:16
  • @pskink i am not worry about hasCode()...we know why we have MessageQueue...to render UI in pipeline Thread...a single thread (UI Thread) is responsible to do this job...if we have more than one Thread having MessageQueue then what about the pipeline concept for rendering...in my concern both should be same. Commented Dec 30, 2014 at 12:16
  • sorry i dont understand you at all, what you wanna do? Commented Dec 30, 2014 at 12:18

1 Answer 1

1

It's not the same MessageQueue, when you call Looper.loop() in a new thread, a new MessageQueue will attache to the new created thread. Then we usually use Handler to communicate with the thread. The main UI thread's MessageQueue is created by system when you app start. You can compare the message queues by their reference.

private static final String TAG = MainActivity.class.getSimpleName();
private Handler mHandler;
private MessageQueue messageQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    messageQueue = Looper.myQueue();
    new Thread() {

        @Override
        public void run() {
            super.run();
            Looper.prepare();
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    if (Looper.myQueue().equals(messageQueue)) {
                        Log.d(TAG, "same message queue");
                    } else {
                        Log.d(TAG, "not the same queue");
                    }
                }
            };
            mHandler.sendEmptyMessage(0);
            Looper.loop();
        }
    }.start();
}

Here is a related discussion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.