ThumbnailDownloader Implementation


Hello everyone,

I have two questions about the ThumbnailDownloader Implementation in the book:
(1) The ThumbnailDownloader class has extended from handlerThread, could anyone explain why we do not need to override the run() method?
(2) Could anyone explain why can we call the ThumbnailDownloader thread’s method from main thread()?

Thank you so much.

Best regards,


1- HandlerThread overrides run() itself to setup Looper. Once the Looper is run, you can use a Handler to execute code on the thread instead of using a run() implementation like you would with a normal thread.

2- The queueThumbnail method is ThumbnailDownloader’s public interface. It’s used to give ThumbnailDownloader work to do.

Let me know if you need any more clarification.


In main thread’s onCreate() method, you use mThumbnailThread.start() to start the mThumbnailThread thread.

and the mThumbnailThread has overriden the thread’s run() method

public class HandlerThread extends Thread {
    public void run() {
        synchronized (this) {
            mLooper = Looper.myLooper();

in this run() method
1.Looper.prepare(); // use a threadLocal instance to connect the mThumbnailThread and its looper

    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    sThreadLocal.set(new Looper(quitAllowed));

2.onLooperPrepared(); // define an mHandler attached to the mThumbnailThread and the handlerMessage() method to handle the download task in the mThumbnailThread, we override this method

protected void onLooperPrepared() {
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == MESSAGE_DOWNLOAD) {
                    Token token = (Token) msg.obj;
                    Log.i(TAG, "Got a request for url: " + requestMap.get(token));

3.Looper.loop(); // if the message queue is blank, the looper will block, else the looper will poll the msg from the message queue and use the’s just the mHandler) to handle it

for (;;) {
            Message msg =; // might block

in main UI thread, GalleryItemAdapter’s getView() method will use mThumbnailThread.queueThumbnail(imageView, item.getUrl()) to push the msg into message queue.

public void queueThumbnail(Token token, String url) {
        Log.i(TAG, "Got an URL: " + url);

        requestMap.put(token, url);

        mHandler.obtainMessage(MESSAGE_DOWNLOAD, token).sendToTarget();

mHandler.obtainMessage() will create a msg, and msg.sendToTarget() will push the msg into the tail of the message queue, because mHandler is created in the mThumbnailThread, the download task will run() on mThumbnailThread using the mHandler.handleMessage()