1. 程式人生 > >android 在專案中使用執行緒池

android 在專案中使用執行緒池

android 中除了更新UI的主執行緒以外,還需要處理業務邏輯的工作執行緒,所以需要我們構建執行緒池,對執行緒進行統一的管理.
android中像訪問記憶體卡,聯網等耗較長的操作時,需要在一部執行緒中使用|
1.編寫一個android使用的執行緒工具類
 

package com.dejun.commonsdk.thread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author:DoctorWei
 * Time:2018/12/22 14:13
 * Description:
 * email:
[email protected]
*/ public class ThreadManager { private static ThreadManager threadManager; /** * 懶漢多執行緒單例 * * @return */ public static ThreadManager getInstance() { if (threadManager == null) { synchronized (ThreadManager.class) { if (threadManager == null) { threadManager = new ThreadManager(); } } } return threadManager; } /** * 核心執行緒池的數量,同時能夠執行的執行緒數量 */ private int corePoolSize; /** * 最大執行緒池數量,表示當緩衝佇列滿的時候能繼續容納的等待任務的數量 */ private int maximumPoolSize; /** * 存活時間 */ private long keepAliveTime = 1; private TimeUnit unit = TimeUnit.HOURS; private ThreadPoolExecutor executor; private ThreadManager() { /** * 給corePoolSize賦值:當前裝置可用處理器核心數*2 + 1,能夠讓cpu的效率得到最大程度執行(有研究論證的) */ corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1; //雖然maximumPoolSize用不到,但是需要賦值,否則報錯 maximumPoolSize = corePoolSize; executor = new ThreadPoolExecutor( //當某個核心任務執行完畢,會依次從緩衝佇列中取出等待任務 corePoolSize, //5,先corePoolSize,然後new LinkedBlockingQueue<Runnable>(),然後maximumPoolSize,但是它的數量是包含了corePoolSize的 maximumPoolSize, //表示的是maximumPoolSize當中等待任務的存活時間 keepAliveTime, unit, //緩衝佇列,用於存放等待任務,Linked的先進先出 new LinkedBlockingQueue<Runnable>(), //建立執行緒的工廠 // Executors.defaultThreadFactory(), new DefaultThreadFactory(Thread.NORM_PRIORITY, "tiaoba-pool-"), //用來對超出maximumPoolSize的任務的處理策略 new ThreadPoolExecutor.AbortPolicy() ); } /** * 執行任務 * * @param runnable */ public void execute(Runnable runnable) { if (executor == null) { //執行緒池執行者。 //參1:核心執行緒數;參2:最大執行緒數;參3:執行緒休眠時間;參4:時間單位;參5:執行緒佇列;參6:生產執行緒的工廠;參7:執行緒異常處理策略 executor = new ThreadPoolExecutor( corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), // Executors.defaultThreadFactory(), new DefaultThreadFactory(Thread.NORM_PRIORITY, "tiaoba-pool-"), new ThreadPoolExecutor.AbortPolicy()); } if (runnable != null) { executor.execute(runnable); } } /** * 移除任務 */ public void remove(Runnable runnable) { if (runnable != null) { executor.remove(runnable); } } /** * 建立執行緒的工廠,設定執行緒的優先順序,group,以及命名 */ private static class DefaultThreadFactory implements ThreadFactory { /** * 執行緒池的計數 */ private static final AtomicInteger poolNumber = new AtomicInteger(1); /** * 執行緒的計數 */ private final AtomicInteger threadNumber = new AtomicInteger(1); private final ThreadGroup group; private final String namePrefix; private final int threadPriority; DefaultThreadFactory(int threadPriority, String threadNamePrefix) { this.threadPriority = threadPriority; this.group = Thread.currentThread().getThreadGroup(); namePrefix = threadNamePrefix + poolNumber.getAndIncrement() + "-thread-"; } @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.isDaemon()) { t.setDaemon(false); } t.setPriority(threadPriority); return t; } } } 2.使用
ThreadManager.getInstance().execute(new Runnable() {
    @Override
    public void run() {
 
    }
});