This section shows how to perform tasks online Cheng Chili. The process is to add a task to the thread pool's work queue, and when the thread is available (perform other tasks, idle, or not yet perform the task), Threadpoolexecutor will take the task from the queue and run it on the thread.
This lesson also shows you how to stop a running task.
Perform tasks on an online Cheng Chili thread
In Threadpoolexecutor.execute (), the incoming Runnable object starts the task. This method adds the task to the thread Pool Task Force column. When there is an idle thread, the manager takes out the longest waiting task and runs on the thread.
Copy Code code 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
Mdecodethreadpool.execute (
Phototask.getphotodecoderunnable ());
...
}
...
}
...
}
When Threadpoolexecutor starts runnable, the run () method is automatically invoked.
Break code that is running
To stop a task, you need to interrupt the process of the task. You need to save a handle of the current thread while creating the task.
Such as:
Copy Code code 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
*/
Mphototask.setimagedecodethread (Thread.CurrentThread ());
...
}
...
}
To interrupt a thread, call Thread.Interrupt (). Tip: Thread objects are system-controlled and can be edited outside of your app process. For this reason, you need to access the lock before interrupting it and put it in a sync block:
Copy Code code 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[mdecodeworkqueue.size ()];
Populates the array with the Runnables in the queue
Mdecodeworkqueue.toarray (Runnablearray);
Stores the array length in order to iterate over the array
int len = runnablearray.length;
/*
* 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) {
Thread.Interrupt ();
}
}
}
}
...
}
In most cases, thread.interrupt () immediately stops the thread. However, it will only stop the thread waiting, but will not interrupt the CPU or the network-intensive task. To prevent the system from slowing down, you should test the request to wait for the interrupt before you start to try the operation.
Copy Code code as follows:
/*
* Before continuing, checks to the Thread hasn ' t
* Been interrupted
*/
if (thread.interrupted ()) {
Return
}
...
Decodes a byte array into a Bitmap (cpu-intensive)
Bitmapfactory.decodebytearray (
Imagebuffer, 0, Imagebuffer.length, bitmapoptions);
...