package com.jy.day15installapkmorethreaddownload;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
- Created by asus on 2019/2/11.
*/
public class ThreadManager {
private static ThreadManager mManager;
private final ThreadPoolExecutor mExecutor;
private ThreadManager(){
mExecutor = new ThreadPoolExecutor(5,//核心線程數(shù)量,核心池的大小
20,//線程池最大線程數(shù)
30,//表示線程沒(méi)有任務(wù)執(zhí)行時(shí)最多保持多久時(shí)間會(huì)終止
TimeUnit.SECONDS,//時(shí)間單位
new LinkedBlockingQueue<Runnable>(),//任務(wù)隊(duì)列,用來(lái)存儲(chǔ)等待執(zhí)行的任務(wù)
Executors.defaultThreadFactory(),//線程工廠,如何去創(chuàng)建線程的
new ThreadPoolExecutor.AbortPolicy());
}
public static ThreadManager getInstance(){
if (mManager == null){
synchronized (ThreadManager.class){
if (mManager == null){
mManager = new ThreadManager();
}
}
}
return mManager;
}
/**
* 執(zhí)行任務(wù)
*/
public void execute(Runnable runnable){
if(runnable==null)return;
mExecutor.execute(runnable);
}
/**
* 從線程池中移除任務(wù)
*/
public void remove(Runnable runnable){
if(runnable==null)return;
mExecutor.remove(runnable);
}
}