Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Basis of Android Threading: Loopers and Handlers

The Basis of Android Threading: Loopers and Handlers

Matheus Cassiano Candido

April 03, 2018
Tweet

More Decks by Matheus Cassiano Candido

Other Decks in Technology

Transcript

  1. Java Threads • Execute their work and quit • Threads

    have their own stack (which is large and memory intensive) • Creating a new thread takes (precious) time and memory
  2. Looper • Creates a message loop for a Thread •

    Interaction with the Looper happens through a Handler • A Looper basically loops indefinitely dequeuing messages until it is requested to quit
  3. class LooperThread extends Thread { public Handler handler; public void

    run() { Looper.prepare(); handler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
  4. public static void loop() { final Looper me = myLooper();

    final MessageQueue queue = me.mQueue; // ... for (;;) { Message msg = queue.next(); // might block // ... try { msg.target.dispatchMessage(msg); } finally { // ... } // ... } }
  5. public static void loop() { final Looper me = myLooper();

    final MessageQueue queue = me.mQueue; // ... for (;;) { Message msg = queue.next(); // might block // ... try { msg.target.dispatchMessage(msg); } finally { // ... } // ... } }
  6. public static void loop() { final Looper me = myLooper();

    final MessageQueue queue = me.mQueue; // ... for (;;) { Message msg = queue.next(); // might block // ... try { msg.target.dispatchMessage(msg); } finally { // ... } // ... } }
  7. Handler • Responsible for sending messages to a Looper •

    Default constructor uses the Looper of the thread it was called on • A different Looper can be passed in as a constructor argument
  8. Handler • A message can be queued using many of

    the obtainMessage and post method overloads val message = handler.obtainMessage(LOAD_SUCCESS, result) message.sendToTarget() or mainThreadHandler.post { // some ui thread related code }
  9. Work on the main thread? • Choreographer does not post

    at the end of the queue but rather at a specific (frame) time • If there’s something blocking the queue at that time, a frame will be dropped!
  10. Work on the main thread? final long nextFrameTime = Math.max(

    mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now); // … Message msg = mHandler.obtainMessage(MSG_DO_FRAME); // … mHandler.sendMessageAtTime(msg, nextFrameTime);
  11. Message & MessageQueue • MessageQueue is not used directly, but

    through a Handler • Message is a public class but should not be instantiated: there’s a pool of recycled message objects (Message.obtain, Handler.obtainMessage)
  12. public final class Message implements Parcelable { public int what;

    public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int sendingUid = -1; }
  13. ActivityThread • Created at the beginning of the app process

    • Orchestrates many intricacies of the app, like launching and coordinating activities life cycles • Basically all operations use the main handler
  14. HandlerThread • It’s wrapper around a Java Thread • A

    HandlerThread assigns itself a looper and prepares it when run() is called • It can also provide its handler (one is not created automatically)