在Android線程池里運(yùn)行代碼任務(wù)實(shí)例
本節(jié)展示如何在線程池里執(zhí)行任務(wù)。流程是,添加一個(gè)任務(wù)到線程池的工作隊(duì)列,當(dāng)有線程可用時(shí)(執(zhí)行完其他任務(wù),空閑,或者還沒(méi)執(zhí)行任務(wù)),ThreadPoolExecutor會(huì)從隊(duì)列里取任務(wù),并在線程里運(yùn)行。
本課同時(shí)向你展示了如何停止正在運(yùn)行的任務(wù)。
在線程池里的線程上執(zhí)行任務(wù)
在ThreadPoolExecutor.execute()里傳入 Runnable對(duì)象啟動(dòng)任務(wù)。這個(gè)方法會(huì)把任務(wù)添加到線程池工作隊(duì)列。當(dāng)有空閑線程時(shí),管理器會(huì)取出等待最久的任務(wù),在線程上運(yùn)行。
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());
...
}
...
}
...
}
當(dāng)ThreadPoolExecutor啟動(dòng)Runnable時(shí),會(huì)自動(dòng)調(diào)用run()方法。
中斷正在運(yùn)行的代碼
要停止任務(wù),你需要中斷任務(wù)的進(jìn)程。你需要在創(chuàng)建任務(wù)的時(shí)候,保存一個(gè)當(dāng)前線程的handle.
如:
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());
...
}
...
}
要中斷線程,調(diào)用Thread.interrupt()就可以了。提示:線程對(duì)象是系統(tǒng)控制的,可以在你的app進(jìn)程外被編輯。因?yàn)檫@個(gè)原因,你需要在中斷它前加訪問(wèn)鎖,放到一個(gè)同步塊里:
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()會(huì)馬上停止線程。可是,它只會(huì)停止在等待的線程,但不會(huì)中斷cpu或network-intensive任務(wù)。為了避免系統(tǒng)變慢,你應(yīng)該在開(kāi)始嘗試操作前測(cè)試等待中斷的請(qǐng)求。
/*
* 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);
...
相關(guān)文章
Android判斷11位手機(jī)號(hào)碼的方法(正則表達(dá)式)
項(xiàng)目里頭需要做一個(gè)判斷用戶輸入的號(hào)碼是否是正確的手機(jī)號(hào)碼,正確的手機(jī)號(hào)碼應(yīng)該是11位的,這里我們需要用一個(gè)正則表達(dá)式來(lái)進(jìn)行判斷,下面我把寫(xiě)法分享給大家2016-12-12Android Handler內(nèi)存泄漏詳解及其解決方案
在android開(kāi)發(fā)過(guò)程中,我們可能會(huì)遇到過(guò)令人奔潰的OOM異常,這篇文章主要介紹了Android Handler內(nèi)存泄漏詳解及其解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08android實(shí)現(xiàn)菜單三級(jí)樹(shù)效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)菜單三級(jí)樹(shù)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android編程設(shè)定activity進(jìn)入和退出效果的方法
這篇文章主要介紹了Android編程設(shè)定activity進(jìn)入和退出效果的方法,簡(jiǎn)單分析了Android Activity進(jìn)入與退出效果的實(shí)現(xiàn)原理及相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07Android 使用XML做動(dòng)畫(huà)UI的深入解析
在Android應(yīng)用程序,使用動(dòng)畫(huà)效果,能帶給用戶更好的感覺(jué)。做動(dòng)畫(huà)可以通過(guò)XML或Android代碼。本教程中,介紹使用XML來(lái)做動(dòng)畫(huà)。在這里,介紹基本的動(dòng)畫(huà),如淡入,淡出,旋轉(zhuǎn)等,需要的朋友可以參考下2013-07-07Android Camera開(kāi)發(fā)手電筒功能
這篇文章主要介紹了Android Camera開(kāi)發(fā)手電筒功能的相關(guān)資料,需要的朋友可以參考下2016-07-07Android圖像視圖ImageView實(shí)現(xiàn)圖像拉伸效果
這篇文章主要為大家詳細(xì)介紹了Android圖像視圖ImageView實(shí)現(xiàn)圖像拉伸演示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)
這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10