3

In Android, Main Thread & HandlerThread has Looper & MessageQueue by default. I can call getLooper() on handlerThread object, but why not on main Thread ?

HandlerThread ht = new HandlerThread();
Looper htLooper = ht.getLooper();      // Works fine

Thread mainThread = Looper.getMainLooper().getThread();
Looper mainLooper = mainThread.getLooper();    // getLooper() doesn't compile.

In a real scenario, one would never need to use getLooper() on mainThread; we can just call Looper.getMainLooper(). I would just like to know why it doesn't work.

I understand it from a Java perspective, that Looper.getMainLooper().getThread() returns a java.lang.Thread, and Thread class doesn't have a getLooper() method; but Android's main thread does. Can Main Thread be accessed as a HandlerThread ?

Ashok Bijoy Debnath
  • 1,493
  • 1
  • 17
  • 27

1 Answers1

2

If you take a look at the source code, you will see that the thread inside the looper is not of type HandlerThread:

60       final Thread mThread;
...
188      mThread = Thread.currentThread();

Can Main Thread be accessed as a HandlerThread

No

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417