Android - multiple threads 多執行緒重用

假如想要載入圖片?
我們可以用 Thread 來做到,且不影響使用者當下的操作。


假如持續有很多張圖片 ?
將不斷創建 Thread,這會使各個 Thread 搶占資源,導致資源不夠用而OOM......


這時候可以用ThreadPoolExecutor來解決,使 Thread 重用

private static ExecutorService fixed;

// newCachedThreadPool 方法
if (fixed == null || fixed.isTerminated() || fixed.isShutdown()) {
    // 無次數限制,但不保證順序性
    fixed = Executors.newCachedThreadPool();
}
if (fixed != null && !fixed.isTerminated() && !fixed.isShutdown()) {
    fixed.execute(new ImageRunnableOneNew(mImageDequeOne));
}

留言

這個網誌中的熱門文章

Android - 輸入字串排除 使用InputFilter

Android - OkHttp3連線 post應用