標籤:
package com.jredu.schooltong.manager;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorManager {
// 1.私人建構函式,提供靜態變數用以儲存,提供靜態方法
private ExecutorManager() {
init();
}
// 靜態變數
private static ExecutorManager instance;
// 靜態方法
public static ExecutorManager getInstance() {
if (instance == null) {
//加鎖
synchronized (ExecutorManager.class) {
if (instance == null) {
instance = new ExecutorManager();
}
}
}
return instance;
}
// 運用單例模式,保證只能夠被執行個體化一次
// 這個類就能應用到其他APP中了
private ExecutorService executorService;
// 初始化線程池
private void init() {
int threadCount = Runtime.getRuntime().availableProcessors() * 2 + 1;
executorService = Executors
.newFixedThreadPool(Math.min(threadCount, 8));
}
public ExecutorService getExecutor(){
return this.executorService;
}
public void execute(Runnable runnable){
this.executorService.execute(runnable);
}
}
Android單例線程池