W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
編寫:AllenZheng1991 - 原文:http://developer.android.com/training/multiple-threads/run-code.html
在前面的課程中向你展示了如何去定義一個可以管理線程池且能在他們中執(zhí)行任務代碼的類。在這一課中我們將向你展示如何在線程池中執(zhí)行任務代碼。為了達到這個目的,你需要把任務添加到線程池的工作隊列中去,當一個線程變成可運行狀態(tài)時,ThreadPoolExecutor從工作隊列中取出一個任務,然后在該線程中執(zhí)行。
這節(jié)課同時也向你展示了如何去停止一個正在執(zhí)行的任務,這個任務可能在剛開始執(zhí)行時是你想要的,但后來發(fā)現(xiàn)它所做的工作并不是你所需要的。你可以取消線程正在執(zhí)行的任務,而不是浪費處理器的運行時間。例如你正在從網絡上下載圖片且對下載的圖片進行了緩存,當檢測到正在下載的圖片在緩存中已經存在時,你可能希望停止這個下載任務。當然,這取決于你編寫APP的方式,因為可能壓在你啟動下載任務之前無法獲知是否需要啟動這個任務。
為了在一個特定的線程池的線程里開啟一個任務,可以通過調用ThreadPoolExecutor.execute(),它需要提供一個Runnable類型的參數(shù),這個調用會把該任務添加到這個線程池中的工作隊列。當一個空閑的線程進入可執(zhí)行狀態(tài)時,線程管理者從工作隊列中取出等待時間最長的那個任務,并且在線程中執(zhí)行它。
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());
...
}
...
}
...
}
當ThreadPoolExecutor在一個線程中開啟一個Runnable后,它會自動調用Runnable的run()方法。
為了停止執(zhí)行一個任務,你必須中斷執(zhí)行這個任務的線程。在準備做這件事之前,當你創(chuàng)建一個任務時,你需要存儲處理該任務的線程。例如:
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());
...
}
...
}
想要中斷一個線程,你可以調用Thread.interrupt())。需要注意的是這些線程對象都被系統(tǒng)控制,系統(tǒng)可以在你的APP進程之外修改 他們。因為這個原因,在你要中斷一個線程時,你需要把這段代碼放在一個同步代碼塊中對這個線程的訪問加鎖來解決這個問題。例如:
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();
}
}
}
}
...
}
在大多數(shù)情況下,通過調用Thread.interrupt()能立即中斷這個線程,然而他只能停止那些處于等待狀態(tài)的線程,卻不能中斷那些占據(jù)CPU或者耗時的連接網絡的任務。為了避免拖慢系統(tǒng)速度或造成系統(tǒng)死鎖,在嘗試執(zhí)行耗時操作之前,你應該測試當前是否存在處于掛起狀態(tài)的中斷請求:
/*
* Before continuing, checks to see that 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);
...
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: