a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

在Android線程池里運行代碼任務實例

2020-04-11 11:49:28
字體:
來源:轉載
供稿:網友

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

在線程池里的線程上執行任務

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

復制代碼 代碼如下:

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

中斷正在運行的代碼

要停止任務,你需要中斷任務的進程。你需要在創建任務的時候,保存一個當前線程的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());
        ...
    }
    ...
}

要中斷線程,調用Thread.interrupt()就可以了。提示:線程對象是系統控制的,可以在你的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();
                }
            }
        }
    }
    ...
}

在大多數案例里,Thread.interrupt()會馬上停止線程。可是,它只會停止在等待的線程,但不會中斷cpu或network-intensive任務。為了避免系統變慢,你應該在開始嘗試操作前測試等待中斷的請求。

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕亚洲欧美日韩在线不卡 | 国产精品综合一区二区 | 亚洲精品视频免费 | 国产色网 | 日韩经典一区 | 久久激情小视频 | 久久人人爽人人爽 | 午夜激情电影在线 | 日本久久艹 | 国产乱码精品一区二区三区忘忧草 | 亚洲欧美一区二区三区在线 | 久久久久国产亚洲日本 | 一区在线观看 | 最近免费中文字幕在线视频2 | 黄色片在线免费播放 | 国产精品热 | 亚州视频在线 | 久久久久久久性 | 中文字幕成人 | 国产成人在线免费观看 | 欧美日本国产欧美日本韩国99 | 黄色自拍视频 | 亚洲三区在线观看 | 久久精品91久久久久久再现 | 91国内视频在线观看 | 欧美视频xxx | 男人天堂成人网 | 国产精品国产精品国产专区不卡 | 韩国精品一区二区三区 | 99爱免费视频 | 国产一页 | 国产成人啪精品午夜在线观看 | av大片在线观看 | 免费av中国| 成人在线免费 | 亚洲一区二区三区日韩 | 免费成人精品 | 亚洲综合在线一区二区 | 日本特黄 | 国产亚洲aaa | 成人av综合 |