线程池的好处
android开发,大家最熟悉的肯定是主线程,也就是ui线程,也都知道在非ui线程更新界面会报错提示不允许在子线程更新ui。但是耗时操作还是需要使用子线程,例如:
new Thread(new Runnable() {}
@Override
public void run() {
// TODO Auto-generated method stub
}
}).start();
感觉方便直接,在任务结束后GC也会自动回收该线程,不过它还在存在很大的弊端
- 如果某个地方需要开启大量的线程。创建线程和销毁线程是需要时间的,这样会导致性能不足
- 线程无法管理,相互竞争导致卡顿或者oom
- 功能太过单一
所以线程池就出现了,我们需要重用线程,要学会使用它:
1.定义一个接口
public interface IThread
{
public void execute(Runnable runnable);
}
2.定义ThreadSingle实现IThread
public class ThreadSingle implements IThread
{
@Override
public void execute(Runnable runnable)
{
new Thread(runnable).start();
}
}
3.定义ThreadPoolProxy类
public class ThreadPoolProxy
{
private ThreadPoolExecutor mExecutor;
int mCorePoolSize;
int mMaximumPoolSize;
long mKeepAliveTime;
public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime)
{
super();
mCorePoolSize = corePoolSize;
mMaximumPoolSize = maximumPoolSize;
mKeepAliveTime = keepAliveTime;
}
private ThreadPoolExecutor initThreadPoolExecutor()
{
if(mExecutor == null)
{
synchronized (ThreadPoolProxy.class)
{
if(mExecutor == null)
{
TimeUnit unit = TimeUnit.MILLISECONDS;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
mExecutor = new ThreadPoolExecutor(
mCorePoolSize,
mMaximumPoolSize,
mKeepAliveTime,
unit,
workQueue,
threadFactory,
handler
);
}
}
}
return mExecutor;
}
public void execute(Runnable task)
{
initThreadPoolExecutor();
mExecutor.execute(task);
}
public Future<?> submit(Runnable task)
{
initThreadPoolExecutor();
return mExecutor.submit(task);
}
public void removeTask(Runnable task)
{
initThreadPoolExecutor();
mExecutor.remove(task);
}
4.定义ThreadPool,实现IThread
public class ThreadPool implements IThread {
private static ThreadPoolProxy mNormalPool = null;
private static ThreadPool _intance = null;
private ThreadPool() {
}
public static ThreadPool getInstance() {
if (_intance == null) {
synchronized (ThreadPool.class) {
_intance = new ThreadPool();
}
}
return _intance;
}
@Override
public void execute(Runnable runnable) {
if (mNormalPool == null) {
synchronized (ThreadPoolProxy.class) {
if (mNormalPool == null) {
mNormalPool = new ThreadPoolProxy(5, 15, 2000);
mNormalPool.execute(runnable);
}
}
} else {
mNormalPool.execute(runnable);
}
}
}
5.定义ThreadSingle,实现ITHread
public class ThreadSingle implements IThread
{
@Override
public void execute(Runnable runnable)
{
new Thread(runnable).start();
}
}
6.定义ThreadPoolFactory
public class ThreadPoolFactory
{
static ThreadPoolProxy mNormalPool;
static ThreadPoolProxy mDownLoadPool;
public static IThread getThread()
{
return ThreadPool.getInstance();
}
public static ThreadPoolProxy getDownLoadPool()
{
if (mDownLoadPool == null)
{
synchronized (ThreadPoolProxy.class)
{
if (mDownLoadPool == null)
{
mDownLoadPool = new ThreadPoolProxy(3, 3, 3000);
}
}
}
return mDownLoadPool;
}
}
在主程序中的使用
ThreadPoolFactory.getThread().execute(new Runnable() {
@Override
public void run() {
//code
}
});
简单方便,当程序调用线程时,会去线程池访问,如果线程池无现有线程或空闲时就会创建,如果存在空闲的线程,不会再重新去创建,调用空闲的线程去执行