Notes about Android thread.

Terminology

Threadopen in new window

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads running concurrently.

Executoropen in new window

An object that executes submitted Runnable tasks.

ExecutorServiceopen in new window

An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.

Implements Executor interface.

ScheduledExecutorServiceopen in new window

An ExecutorService that can schedule commands to run after a given delay, or to execute periodically.

Extends from ExecutorService.

ThreadPoolExecutoropen in new window

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Extends from ExecutorService.

ThreadFactoryopen in new window

An object that creates new threads on demand.

Executorsopen in new window

Factory and utilty methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory and Callable classes.

Handleropen in new window

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. There are two main uses for a Handler:

  1. to schedule messages and runnables to be executed at some point in the future
  2. to enqueue an action to be performed on a different thread than your own.
ConceptDefinitionUsage
UI Thread (Main Thread)A thread usually to operate UI.Handle UI release events
Messageopen in new windowDefines a message containing a description and arbitrary data object that can be sent to a HandlerData structure need to be transferred.
MessageQueueopen in new windowLow-Level class holding the list of messages to be dispatched by a Looper.Save the Message send by Handler
Handleropen in new windowA Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue.1. Add Message to the MessageQueue
2. Loop the dispatched Message
Looperopen in new windowClass used to run a message loop for a threadLoop message
  • Every thread only have one Looper;
  • One looper can bind to multiple thread;

Code Examples

1. How to submit a task on UI thread?

public void runOnUIThread(Runnable runnable) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        runnable.run();
    } else {
        (new Handler(Looper.getMainLooper())).post(runnable);
    }
}

2. How to submit a task on other thread?

2.1 Create a new thread and submit a runnable task

public void submitTaskAsync(Runnable runnable) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            runnable.run();
        }
    }).start();
}

2.2 Creat a single thread executor and submit a runnable task sequentially

private final ExecutorService serialQueue = Executors.newSingleThreadExecutor();

public void submitTaskAsync(Runnable runnable) {
    this.serialQueue.submit(new Runnable() {
        @Override
        public void run() {
            runnable.run();
        }
    });
}

3. How to communicate from work thread to UI thread?

3.1 Send message from work thread to UI thread by Handler.sendMessage()

public class ThreadDemo {
    // 1. define handler
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 4. operate message at UI thread
            switch (msg.what) {
                case 1: 
                    // do something
                    break;
                default:
                    break;
            }
        }
    };
    
    private void fetchDadaAndUpdateOnUIThread() {
        // Start a new thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Mock do something for a long time ...
                Thread.sleep(1000);
              
                // 2. Create Message 
                Bundle bundle = new Bundle();
                bundle.putString("key", "value");
                
                Message msg = new Message();
                msg.setData(bundle);
                msg.what = 1;
                
                // 3. Send Message
                mHandler.sendMessage(msg);
            }
        }).start();
    }
}

3.2 Send message from work thread to UI thread by Handler.post()

private void notifyOnUIThread() {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            // do some work on UI thread
        }
    });
}