前言:
主線程和子線程
主線程(也叫UI線程):在java中默認(rèn)情況下一個進程只有一個線程,這個線程就是主線程。其主要作用是處理和界面交互相關(guān)的邏輯,主線程不能做太多耗時的操作,否則會產(chǎn)生界面卡頓的感覺。為保持較快的響應(yīng)速度,子線程就出場了。
子線程:也叫工作線程,除了主線程之外的都是子線程。
基本用途:主線程是運行四大組件及處理它們和用戶的交互,子線程處理耗時的任務(wù),如網(wǎng)絡(luò)請求、I/O操作等。
gitHub 地址:https://github.com/lyyRunning/ThreadDemo
Android中的線程表現(xiàn)形式:
Thread、AsyncTask、HanderThread、IntentService.
簡單介紹一下后三者的注意事項:
- AysncTask: 可理解為輕量級的線程池,異步任務(wù)類,其封裝了Thread和Handler,通過AysncTask可以很方便的執(zhí)行后臺任務(wù)及在主線程中訪問UI.
缺點:不適合進行特別耗時的后臺任務(wù),對于特別耗時的任務(wù)最好 用池線程池替換。
HandlerThread: 繼承自Thread,它是一種可以 使用Handler和Thread.實現(xiàn)也很簡單,在run方法中通過Looper.prepare()來創(chuàng)建消息隊列,并通過Looper.loop()來開啟消息循環(huán)。
IntentService: 繼承自Service,是一個抽象類,所以必須創(chuàng)建他的子類才能使用IntentSerivce,可用于執(zhí)行后臺耗時任務(wù),當(dāng)任務(wù)完成后會自動停止,該優(yōu)先級高于其他單純的線程,不容易補系統(tǒng)殺死。 IntentSerivce 封裝了Handler和HandlerThread.
線程池的好處:
- 重用線程池中的線程,避免因為線程的創(chuàng)建和銷毀所帶來的性能消耗。
- 能有效控制線程池的最大并發(fā)數(shù),避免大量的線程之間因相互搶占系統(tǒng)資源而導(dǎo)致的阻塞現(xiàn)象。
- 能對線程進行簡單的管理,并提供定時執(zhí)行以及指定間隔循環(huán)執(zhí)行等功能。
Android中的線程池來源于java,主要是通過Executor 來派生特定類型的線程池,不同類型的線程池有不同的意義。Executor是一個接口,真正地實現(xiàn)為ThreadPoolExecutor,他提供了一系列的參數(shù)來配置線程池。不同參數(shù)意味著不同的線程池。
一、ThreadPoolExecutor
ThreadPoolExecutor是線程池的真正實現(xiàn),它的構(gòu)造方法提供了一系列參數(shù)來配置線程池。下面介紹ThreadPoolExecutor的構(gòu)造方法中各個參數(shù)的含義,這些參數(shù)將會直接影響線程池的功能特性,下面是ThreadPoolExecutor的一個比較常用的構(gòu)造方法。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingDeque<Runnable> workQueue,
ThreadFactory threadFactory)
corePoolSize
線程池的核心數(shù),默認(rèn)情況下,核心線程會在線程池中一直存活,即使他們處于閑置狀態(tài)。如果將ThreadPoolExecutor的 allowCoreThreadTimeOut 屬性設(shè)置為 true,那么閑置的核心線程在等待新任務(wù)到來時會有超時策略,這個時間間隔由 KeepAliveTime 所指定,當(dāng)?shù)却龝r間超過KeepAliveTime所指定的時長后,核心線程就會終止。maximumPoolSize
線程池所能容納的最大線程數(shù),當(dāng)活動線程數(shù)達到這個數(shù)值后,后續(xù)的新任務(wù)將會被阻塞。keepAliveTime
非核心線程閑置時超時時長,超過這個時長,非核心線程就會被回收。當(dāng) ThreadPool-Executor 的allowCoreThreadTimeOut屬性設(shè)置為 true 時,keepAliveTime同樣作用于核心線程。unit
用于指定keepAliveTime參數(shù)的時間單位,這是一個枚舉,常用的有 TimeUnit.MILLISECONDS(毫秒)、TimeUnit.SECONDS (秒)以及TimeUnit.MINUTES(分鐘)等。workQueue
線程池中的任務(wù)隊列,通過線程池的 execute 方法提交的 Runnable 對象會存儲在這個參數(shù)中。threadFactory
線程工廠,為線程池提供創(chuàng)建新線程的功能。threadFactory是一個接口,它只有一個方法:Thread new Thread(Runnable r)。
注意:
除了以上這些參數(shù)外,ThreadPoolExecutor 還有一個不常用的參數(shù)Rejected-ExecutionHandler handler。當(dāng)線程池?zé)o法執(zhí)行新任務(wù)時,這可能是由于任務(wù)隊列已滿或者是無法成功執(zhí)行任務(wù),這個時候ThreadPoolExecutor會調(diào)用 handler 的rejectedExecution方法來通知調(diào)用者,默認(rèn)情況下rejectedExecution會直接拋出一個Rejected-Execution-Exception。ThreadPoolExecutor為RejectedExecutionHandler提供了幾個可選的值:CallerRunsPolicy、AbortPolicy、DiscardPolicy 和DiscardOldestPolicy,其中AbortPolicy是默認(rèn)值,它會直接拋出RejectedExecutionException。
- ThreadPoolExecutor執(zhí)行任務(wù)時遵循如下原則:
- 如果線程池中的線程數(shù)量未達到核心線程的數(shù)量,那么會直接啟動一個核心線程來執(zhí)行任務(wù)。
- 如果線程池中的數(shù)量已經(jīng)達到或者超過核心線程的數(shù)量,那么任務(wù)會被插入到任務(wù)隊列中排隊等待執(zhí)行。
- 如果在步驟 2 中無法將任務(wù)插入到任務(wù)隊列中,這往往是由于任務(wù)隊列已滿,這個時候如果線程數(shù)量未達到線程池規(guī)定的最大值,那么會立刻啟動一個非核心線程來執(zhí)行。
4.如果步驟 3 中線程數(shù)量已經(jīng)達到線程池規(guī)定的最大值,那么就拒絕執(zhí)行此任務(wù),ThreadPoolExecutor會調(diào)用RejectedExecutionHandler的rejectedExecution方法來通知調(diào)用者。
- ThreadPoolExecutor的參數(shù)配置在 AsyncTask 中有明顯的體現(xiàn),下面試AsyncTask中的線程池的配置情況:
private static final String LOG_TAG = "AsyncTask";
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
// We want at least 2 threads and at most 4 threads in the core pool,
// preferring to have 1 less than the CPU count to avoid saturating
// the CPU with background work
private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final int KEEP_ALIVE_SECONDS = 30;
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
}
};
private static final BlockingQueue<Runnable> sPoolWorkQueue =
new LinkedBlockingQueue<Runnable>(128);
/**
* An {@link Executor} that can be used to execute tasks in parallel.
*/
public static final Executor THREAD_POOL_EXECUTOR;
static {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
sPoolWorkQueue, sThreadFactory);
threadPoolExecutor.allowCoreThreadTimeOut(true);
THREAD_POOL_EXECUTOR = threadPoolExecutor;
}
- 從以上代碼可知:AsyncTask 對ThreadPoolExecutor這個線程池的配置,規(guī)則如下:
- 核心線程數(shù)等于 CPU 核心數(shù)+1;
- 線程池的最大線程數(shù)為 CPU 核心數(shù)的 2 倍+1;
- 核心線程無超時機制,非核心線程在閑置時的超時時間為 1 秒;
- 任務(wù)隊列的容量為 128;
二、線程池的分類:
Android 中常見的四類具有不同功能特性的線程池,它們都直接或間接地通過配置 ThreadPoolExecutor 來實現(xiàn)自己的功能特性,這四類線程池分別是 FixedThreadPool、CachedThreadPool、ScheduledThreadPool以及SingleThreadExecutor。
1. FixedThreadPool()
通過 Executor 的 newFixedThreadPool方法來創(chuàng)建。它是一種線程數(shù)量固定的線程池,當(dāng)線程處于空閑狀態(tài)時,它們并不會被回收,除非線程池關(guān)閉了。當(dāng)所有的線程都處于活動狀態(tài)時,新任務(wù)都會處于等待狀態(tài),直到有線程空閑出來。由于FixedThreadPool只有核心線程并且這些核心線程不會被回收,這意味著它能更加快速地響應(yīng)外界的請求。newFixedThreadPool方法的實現(xiàn)如下,可以發(fā)現(xiàn)FixedThreadPool中只有核心線程并且這些核心線程沒有超時機制,另外任務(wù)隊列也是沒有大小限制的。
線程池特點:
①可控制線程最大并發(fā)數(shù)(線程數(shù)固定)
②超出的線程會在隊列中等待
使用優(yōu)勢:能夠更快的響應(yīng)外界的請求。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L,TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
2. CachedThreadPool()
通過 Executor 的 newCachedThreadPool方法來創(chuàng)建。它是一種線程數(shù)量不定的線程池,它只有非核心線程,并且其最大線程數(shù)為 Integer.MAX_VALUE。由于Integer.MAX_VALUE是一個很大的數(shù),實際上就相當(dāng)于最大線程數(shù)可以任意大。當(dāng)線程池中的線程都處于活動狀態(tài)時,線程池會創(chuàng)建新的線程池來處理新任務(wù),否則就會利用空閑的線程來處理新任務(wù)。線程池中的空閑線程都有超時機制,這個超時時長是 60 秒,超過 60 秒閑置的線程就會被回收掉。
- 它適合大量的耗時較少的任務(wù);
- 當(dāng)它整個線程池都處于閑置狀態(tài),線程池的線程都會超時被停掉,CachedThreadPool
中沒有任何線程,它幾乎不占用任何系統(tǒng)資源。
緩存線程池的特點:
① 線程數(shù)無限制
② 沒有核心線程,都是非核心線程
優(yōu)勢:比較適合用來執(zhí)行大量的但是耗時較少的任務(wù)。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
3. ScheduledThreadPool()
通過 Executor 的 newScheduledThreadPool方法來創(chuàng)建。它的核心線程數(shù)量是固定的,而非核心線程的數(shù)量是沒有限制的,并且當(dāng)非核心線程閑置時會被立即回收。ScheduledThreadPool這類線程池主要用于執(zhí)行定時任務(wù)和具有固定周期的重復(fù)任務(wù)。
定時線程池特點:
核心線程數(shù)量固定、非核心線程數(shù)量無限制(閑置時馬上回收)
使用場景:
用于執(zhí)行定時任務(wù)和具有固定周期的重復(fù)任務(wù)。
// 1. 創(chuàng)建 定時線程池對象 & 設(shè)置線程池線程數(shù)量固定為4
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4);
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務(wù)
Runnable task =new Runnable(){
public void run(){
System.out.println("開始執(zhí)行任務(wù)");
}
};
// 3. 向線程池提交任務(wù):schedule()
scheduledThreadPool.schedule(task, 1, TimeUnit.SECONDS); // 延遲1s后執(zhí)行任務(wù)
scheduledThreadPool.scheduleAtFixedRate(task,10,1000,TimeUnit.MILLISECONDS);// 延遲10ms后、每隔1000ms執(zhí)行任務(wù)
// 4. 關(guān)閉線程池
scheduledThreadPool.shutdown();
4. SingleThreadPool()
通過 Executor 的 new SingleThreadExecutor方法來創(chuàng)建。這類線程池內(nèi)部只有一個核心線程,它確保所有的任務(wù)都在同一個線程中按順序執(zhí)行。SingleThreadExecutor的意義在于統(tǒng)一所有的外界任務(wù)到一個線程中,這使得在這些任務(wù)之間不需要處理線程同步的問題。
單線程化線程池特點:
只有一個核心線程(保證所有任務(wù)按照指定順序在一個線程中執(zhí)行,不需要處理線程同步的問題)
使用場景:
按順序執(zhí)行,不需要處理同步的問題。不適合并發(fā)但可能引起IO阻塞性及影響UI線程響應(yīng)的操作,如數(shù)據(jù)庫操作,文件操作等
// 1. 創(chuàng)建單線程化線程池
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務(wù)
Runnable task =new Runnable(){
public void run(){
System.out.println("開始執(zhí)行任務(wù)");
}
};
// 3. 向線程池提交任務(wù):execute()
singleThreadExecutor.execute(task);
// 4. 關(guān)閉線程池
singleThreadExecutor.shutdown();
三、線程池的使用:
1.線程池的封裝
public class ExecutorServiceCreator {
/**
* 私有構(gòu)造
*/
private ExecutorServiceCreator() {
}
/**
* 靜態(tài)內(nèi)部類
*/
private static class FixedHolder{
private static final ExecutorService fixedExecutorService = newFixedThreadPool(8);
}
/**
* FixedThreadPool線程池
* @return
*/
public static ExecutorService getFixedInstance(){
return FixedHolder.fixedExecutorService;
}
/**
* 靜態(tài)內(nèi)部類
*/
private static class CachedHolder{
private static final ExecutorService cachedExecutorService = newCachedThreadPool();
}
/**
* CachedThreadPool線程池
* @return
*/
public static ExecutorService getCachedInstance(){
return CachedHolder.cachedExecutorService;
}
/**
* 靜態(tài)內(nèi)部類
*/
private static class ScheduledHolder{
private static final ExecutorService scheduledExecutorService = newCachedThreadPool();
}
/**
* CachedThreadPool線程池
* @return
*/
public static ExecutorService getScheduledInstance(){
return ScheduledHolder.scheduledExecutorService;
}
/**
* 靜態(tài)內(nèi)部類
*/
private static class SingletonHolder {
private static final ExecutorService singletonExecutorService = (ExecutorService) newCachedThreadPool();
}
/**
* CachedThreadPool線程池
* @return
*/
public static ExecutorService getSingleInstance() {
return SingletonHolder.singletonExecutorService;
}
}
2.線程池的調(diào)用:
public class SecondActivity extends AppCompatActivity {
@BindView(R.id.bt_fixed)
Button btFixed;
@BindView(R.id.bt_cached)
Button btCached;
@BindView(R.id.bt_scheduled)
Button btScheduled;
@BindView(R.id.bt_single)
Button btSingle;
@BindView(R.id.bt_next)
Button btNext;
/**
* 聲明自動生成
* 類似 findViewById 的效果
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btNext.setVisibility(View.GONE);
}
/**
* 下面整個方法自動生成
*
* @param view
*/
@OnClick({R.id.bt_fixed, R.id.bt_cached, R.id.bt_scheduled, R.id.bt_single,R.id.bt_next})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.bt_fixed:
fixedInit();
break;
case R.id.bt_cached:
cachedInit();
break;
case R.id.bt_scheduled:
scheduledInit();
break;
case R.id.bt_single:
singleInit();
break;
default:
}
}
private void scheduledInit() {
ExecutorServiceCreator.getScheduledInstance().execute(new Runnable() {
@Override
public void run() {
Log.d("LUO", "scheduledInit======333");
}
});
}
private void cachedInit() {
ExecutorServiceCreator.getCachedInstance().execute(new Runnable() {
@Override
public void run() {
Log.d("LUO", "cachedInit======222");
}
});
}
private void singleInit() {
ExecutorServiceCreator.getSingleInstance().execute(new Runnable() {
@Override
public void run() {
Log.d("LUO", "singleInit======444");
}
});
}
private void fixedInit() {
ExecutorServiceCreator.getFixedInstance().execute(new Runnable() {
@Override
public void run() {
Log.d("LUO", "fixedInit======111");
}
});
}
public static void launch(Context mContext) {
Intent intent = new Intent(mContext,SecondActivity.class);
mContext.startActivity(intent);
}
}