SoFunction
Updated on 2025-04-08

Run code task instance in Android thread pool

This section shows how to perform tasks in a thread pool. The process is to add a task to the thread pool's work queue. When a thread is available (executing other tasks, idle, or not executing tasks yet), ThreadPoolExecutor will take tasks from the queue and run them in the thread.
This lesson also shows you how to stop running tasks.

Execute tasks on threads in thread pool

Pass the Runnable object in () to start the task. This method adds tasks to the thread pool work queue. When there are free threads, the manager will fetch the longest waiting task and run it on the thread.

Copy the codeThe code is as follows:

public class PhotoManager {
    public void handleState(PhotoTask photoTask, int state) {
        switch (state) {
            // The task finished downloading the image
            case DOWNLOAD_COMPLETE:
            // Decodes the image
                (
                        ());
            ...
        }
        ...
    }
    ...
}

When ThreadPoolExecutor starts Runnable, the run() method will be called automatically.

Interrupt running code

To stop a task, you need to interrupt the process of the task. You need to save a handle of the current thread when creating the task.
like:

Copy the codeThe code is as follows:

class PhotoDecodeRunnable implements Runnable {
    // Defines the code to run for this task
    public void run() {
        /*
         * Stores the current Thread in the
         * object that contains PhotoDecodeRunnable
         */
        (());
        ...
    }
    ...
}

To interrupt the thread, just call(). Tip: Thread objects are system-controlled and can be edited outside your app process. For this reason, you need to add an access lock before interrupting it and put it in a synchronization block:

Copy the codeThe code is as follows:

public class PhotoManager {
    public static void cancelAll() {
        /*
         * Creates an array of Runnables that's the same size as the
         * thread pool work queue
         */
        Runnable[] runnableArray = new Runnable[()];
        // Populates the array with the Runnables in the queue
        (runnableArray);
        // Stores the array length in order to iterate over the array
        int len = ;
        /*
         * Iterates over the array of Runnables and interrupts each one's Thread.
         */
        synchronized (sInstance) {
            // Iterates over the array of tasks
            for (int runnableIndex = 0; runnableIndex < len; runnableIndex++) {
                // Gets the current thread
                Thread thread = runnableArray[taskArrayIndex].mThread;
                // if the Thread exists, post an interrupt to it
                if (null != thread) {
                    ();
                }
            }
        }
    }
    ...
}

In most cases, () will stop the thread immediately. However, it will only stop the waiting thread, but will not interrupt the CPU or network-intensive tasks. To avoid slowing the system, you should test the request waiting for interruptions before starting the attempt.

Copy the codeThe code is as follows:

/*
 * Before continuing, checks to see that the Thread hasn't
 * been interrupted
 */
if (()) {
    return;
}
...
// Decodes a byte array into a Bitmap (CPU-intensive)
(
        imageBuffer, 0, , bitmapOptions);
...