Share
Embed
The Basis of Android Threading: Loopers and Handlers
Matheus Cassiano Candido
April 03, 2018
Transcript
18 slides
-
The Basis of Android Threading: Loopers and Handlers Matheus C.
Candido Android @ Concrete -
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 -
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 -
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(); } } -
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 { // ... } // ... } } -
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 { // ... } // ... } } -
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 { // ... } // ... } } -
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 -
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 } -
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! -
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); -
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) -
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; } -
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 -
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) -
Resources • Android OS classes • Um passeio com Threads
no Android — Parte 1