在android开发中经常会使用多线程异步来处理相关任务,而如果用传统的new Thread来创建一个子线程进行处理,会造成一些严重的问题:
1:在任务众多的情况下,系统要为每一个任务创建一个线程,而任务执行完毕后会销毁每一个线程,所以会造成线程频繁地创建与销毁。
2:多个线程频繁地创建会占用大量的资源,并且在资源竞争的时候就容易出现问题,同时这么多的线程缺乏一个统一的管理,容易造成界面的卡顿。
3 : 多个线程频繁地销毁,会频繁地调用GC机制,这会使性能降低,又非常耗时。
总而言之:频繁地为每一个任务创建一个线程,缺乏统一管理,降低性能,并且容易出现问题。
为了解决这些问题,就要用到今天的主角——线程池.
线程池使用的好处:
1:对多个线程进行统一地管理,避免资源竞争中出现的问题。
2:(重点):对线程进行复用,线程在执行完任务后不会立刻销毁,而会等待另外的任务,这样就不会频繁地创建、销毁线程和调用GC。
3:JAVA提供了一套完整的ExecutorService线程池创建的api,可创建多种功能不一的线程池,使用起来很方便。
常见的几种线程池:
ThreadPoolExecutor 创建基本线程池
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize: 该线程池中核心线程的数量。
maximumPoolSize:该线程池中最大线程数量。
keepAliveTime:从字面上就可以理解,是非核心线程空闲时要等待下一个任务到来的时间,当任务很多,每个任务执行时间很短的情况下调大该值有助于提高线程利用率。注意:当allowCoreThreadTimeOut属性设为true时,该属性也可用于核心线程。
unit:上面时间属性的单位
workQueue:任务队列。
threadFactory:线程工厂,可用于设置线程名字等等,一般无须设置该参数。
每2s打印三个日志
//创建基本线程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,5,1,TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(100));
for(int i = 0;i<30;i++){
final int finali = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Log.d("Thread", "run: "+finali);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
threadPoolExecutor.execute(runnable);
}
1> newFixedThreadPool 可以控制最大并发数的线程池。超过最大并发数的线程进入等待队列。(可重用固定线程数)
每2s打印一次任务
//创建fixed线程池
final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
/**
* fixed线程池
*/
for (int i = 0; i < 30; i++) {
final int finali = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Log.d("Thread", "run: " + finali);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
fixedThreadPool.execute(runnable);
}
}
2>newCacheThreadPool 一个无限大并可以缓存的线程池。在之前创建新线程的时刻首先复用已经创建过的空闲线程。
过2s后直接打印30个任务
//创建cached线程池
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
/**
* cached线程池
*/
for (int i = 0; i < 30; i++) {
final int finali = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Log.d("Thread", "run: " + finali);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
cachedThreadPool.execute(runnable);
}
}
3>newScheduledThreadPool 一个可以定时执行的线程池。 适用:周期性执行任务的场景
延时3秒后执行:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
Log.d("newScheduledThreadPool","延期3秒执行");
}
},3, TimeUnit.SECONDS);
延时1秒后每隔3秒执行一次:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Log.d("newScheduledThreadPool","延时1s后每3s执行一次");
}
},1,3,TimeUnit.SECONDS);
4>newSingleThreadExecutor 单线程化线程池。
每2s打印一个任务,由于只有一个核心线程,当被占用时,其他的任务需要进入队列等待。
//创建Single线程池
final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
/**
* single线程池
*/
for (int i = 0; i < 30; i++) {
final int finali = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Log.d("Thread", "run: " + finali);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
singleThreadExecutor.execute(runnable);
https://www.jianshu.com/p/7b2da1d94b42
https://blog.csdn.net/night_dragon/article/details/54409997