国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

在Android線程池里運行代碼任務(wù)實例

2020-04-11 11:49:28
字體:
供稿:網(wǎng)友

本節(jié)展示如何在線程池里執(zhí)行任務(wù)。流程是,添加一個任務(wù)到線程池的工作隊列,當有線程可用時(執(zhí)行完其他任務(wù),空閑,或者還沒執(zhí)行任務(wù)),ThreadPoolExecutor會從隊列里取任務(wù),并在線程里運行。
本課同時向你展示了如何停止正在運行的任務(wù)。

在線程池里的線程上執(zhí)行任務(wù)

在ThreadPoolExecutor.execute()里傳入 Runnable對象啟動任務(wù)。這個方法會把任務(wù)添加到線程池工作隊列。當有空閑線程時,管理器會取出等待最久的任務(wù),在線程上運行。

復(fù)制代碼 代碼如下:

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時,會自動調(diào)用run()方法。

中斷正在運行的代碼

要停止任務(wù),你需要中斷任務(wù)的進程。你需要在創(chuàng)建任務(wù)的時候,保存一個當前線程的handle.
如:

復(fù)制代碼 代碼如下:

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()就可以了。提示:線程對象是系統(tǒng)控制的,可以在你的app進程外被編輯。因為這個原因,你需要在中斷它前加訪問鎖,放到一個同步塊里:

復(fù)制代碼 代碼如下:

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()會馬上停止線程。可是,它只會停止在等待的線程,但不會中斷cpu或network-intensive任務(wù)。為了避免系統(tǒng)變慢,你應(yīng)該在開始嘗試操作前測試等待中斷的請求。

復(fù)制代碼 代碼如下:

/*
 * 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);
...

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪江市| 宁晋县| 双峰县| 屏南县| 双鸭山市| 拜城县| 桦南县| 扎兰屯市| 郎溪县| 台北市| 洛阳市| 曲沃县| 临澧县| 区。| 德保县| 宜宾县| 天津市| 镇宁| 邯郸市| 尉犁县| 阿合奇县| 昌吉市| 高州市| 信宜市| 高邑县| 岑溪市| 建昌县| 织金县| 乌苏市| 义乌市| 皋兰县| 封开县| 韶关市| 临邑县| 丹巴县| 海门市| 临夏县| 肥东县| 宁远县| 如东县| 嘉兴市|