package com.megvii.livecheck.common;
import android.util.Log;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
/**
-
Created by liangzi on 2016/3/17.
/
public class ThreadManager {
private static ThreadManager mInstance;
/*- 线程池,存放临时工作的线程
/
private ExecutorService mTempThreadService;
private ThreadPoolExecutor mSingleThreadPool;
/* - 线程池,存放一直在工作的线程
*/
public ThreadPoolExecutor mStealWorkThreadPool;
public ThreadManager() {
mTempThreadService = newWorkTempPool();
mSingleThreadPool = newSingleThreadPool();
mStealWorkThreadPool = newStealWorkPool();
}public static ThreadManager getInstance() {
if (mInstance == null) {
mInstance = new ThreadManager();
}
return mInstance;
}/**
- 核心线程池的数量,同时能够执行的线程数量
/
private int corePoolSize;
/* - 最大线程池数量,表示当缓冲队列满的时候能继续容纳的等待任务的数量
/
private int maximumPoolSize;
/* - 存活时间
*/
private long keepAliveTime = 0L;
public ThreadPoolExecutor newStealWorkPool() {
corePoolSize = Runtime.getRuntime().availableProcessors() * 2 + 1;
maximumPoolSize = corePoolSize;
return new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new DefaultThreadFactory(Thread.NORM_PRIORITY, “livecheck-pool-”));
}/**
- 存放单一线程的线程池,用于录制视频
- @return
*/
private ThreadPoolExecutor newSingleThreadPool() {
return new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
/***
- 添加线程到线程池
- @param runnable
*/
public void addSingleThread(Runnable runnable) {
mSingleThreadPool.execute(runnable);
}
public void addStealWorkTask(Runnable task) {
mStealWorkThreadPool.execute(task);
}public void removeStealWorkTask(Runnable task) {
// if (task != null) {
if (task != null && mStealWorkThreadPool != null) {
mStealWorkThreadPool.remove(task);
}
}/**
- 移除一个线程
- @param runnable
*/
public void removeSingleThread(Runnable runnable) {
mSingleThreadPool.remove(runnable);
}
/**
- 创建线程池,存放临时工作的线程
*/
private ExecutorService newWorkTempPool() {
return Executors.newCachedThreadPool();
}
/**
- 添加线程到线程池,存放临时工作的线程
*/
public void addWorkTempPool(Runnable run) {
if (mTempThreadService == null) {
mTempThreadService = newWorkTempPool();
}
mTempThreadService.execute(run);
}
/**
- 关掉临时的线程
*/
public void releaseWorkTempPool() {
if (null != mTempThreadService) {
mTempThreadService.shutdown();
mTempThreadService = null;
}
}
/**
- 关掉线程
*/
public void releaseStealWorkPool() {
if (null != mStealWorkThreadPool) {
mStealWorkThreadPool.shutdown();
mStealWorkThreadPool = null;
}
}
/**
-
创建线程的工厂,设置线程的优先级,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;
}
} - 线程池的计数
- 线程池,存放临时工作的线程
}