Android開發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線程與線程池)(推薦)
一、線程
在Android開發(fā)中,你不可能都在主線程中開發(fā),畢竟要聯(lián)網(wǎng),下載數(shù)據(jù),保存數(shù)據(jù)等操作,當(dāng)然這就離不開線程。
(當(dāng)然你可以在Android4.0以前的手機(jī)里在主線程請求網(wǎng)絡(luò),我最早開發(fā)的時(shí)候,用的手機(jī)比較古老。。。)
在Android中你可以隨意創(chuàng)建線程,于是就會(huì)造成線程不可控,內(nèi)存泄漏,創(chuàng)建線程消耗資源,線程太多了消耗資源等問題。
具體線程怎么創(chuàng)建我就不在文章里描述了,畢竟這主要將并發(fā)編程。。。。
大家知道線程不可控就好了。。。于是就需要對線程進(jìn)行控制,防止一系列問題出現(xiàn),這就用到了如下要講的東西。
二、線程池
線程池:顧名思義,就是放線程的大池子。
如何創(chuàng)建一個(gè)線程池?
先說說幾個(gè)系統(tǒng)的線程池:
- FixedThreadPool 創(chuàng)建定長線程的線程池
- CachedThreadPool 需要的時(shí)候建立新的線程,超時(shí)線程銷毀
- SingleThreadPool 單個(gè)線程的線程池
- ScheduledThreadPool 可以定時(shí)的線程池,創(chuàng)建周期性的任務(wù)
這幾個(gè)線程池不做多余闡述,因?yàn)檫@些線程池的原理都與我下面要講的有關(guān)。。。。
如何自定義線程池(先來了解幾個(gè)必須知道的參數(shù)):
corePoolSize:
核心線程池大小,線程池中主要工作的線程的多少。
maximumPoolSize:
線程池最大線程數(shù)。
keepAliveTime:
空閑線程可保持的時(shí)間是多久,如果你啟用了allowCoreThreadTimeOut方法,你的線程池里的空閑線程在這個(gè)時(shí)間段后會(huì)自動(dòng)銷毀,如果沒啟用,則只要不超過corePoolSize,空閑線程也不會(huì)銷毀。
Unit:
keepAliveTime的時(shí)間單位
workQueue:
阻塞隊(duì)列,當(dāng)任務(wù)達(dá)到corePoolSize,就會(huì)被放入這個(gè)隊(duì)列
常見幾種BlockingQueue實(shí)現(xiàn)
- ArrayBlockingQueue : 有界的數(shù)組隊(duì)列
- LinkedBlockingQueue : 可支持有界/無界的隊(duì)列,使用鏈表實(shí)現(xiàn)
- PriorityBlockingQueue : 優(yōu)先隊(duì)列,可以針對任務(wù)排序
- SynchronousQueue : 隊(duì)列長度為1的隊(duì)列,和Array有點(diǎn)區(qū)別就是:client thread提交到block queue會(huì)是一個(gè)阻塞過程,直到有一個(gè)worker thread連接上來poll task。
threadFactory:
線程工廠,主要用來創(chuàng)建線程;
handler:
表示當(dāng)拒絕處理任務(wù)時(shí)的策略,也就是參數(shù)maximumPoolSize達(dá)到后丟棄處理的方法。有以下四種取值:
- ThreadPoolExecutor.AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常。
- ThreadPoolExecutor.DiscardPolicy:也是丟棄任務(wù),但是不拋出異常。
- ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù),然后重新嘗試執(zhí)行任務(wù)(重復(fù)此過程)
- ThreadPoolExecutor.CallerRunsPolicy:由調(diào)用線程處理該任務(wù)
用戶也可以實(shí)現(xiàn)接口RejectedExecutionHandler定制自己的策略。
代碼展示:
//線程工廠 public class TaskThreadFactory implements ThreadFactory { private final AtomicInteger mThreadNumber = new AtomicInteger(1); private final String mNamePrefix; TaskThreadFactory(String name) { mNamePrefix = name + "#"; } public Thread newThread(Runnable r) { Thread t = new Thread(r,mNamePrefix + mThreadNumber.getAndIncrement()); // if (t.isDaemon()) // t.setDaemon(false); // // if (t.getPriority() != Thread.NORM_PRIORITY) // t.setPriority(Thread.NORM_PRIORITY); return t; } } //重寫runnable public class PRunnable implements Runnable { public static final int HIGH = 1;//優(yōu)先級高 public static final int NORMAL = 2;//優(yōu)先級中等 public static final int LOW = 3;//優(yōu)先級低 @IntDef({HIGH,NORMAL,LOW}) @Retention(RetentionPolicy.SOURCE) public @interface Priority{} public final int priority; private final Runnable runnable; public int serial; public PRunnable(Runnable runnable){ this(NORMAL,runnable); } public PRunnable(@Priority int priority,Runnable runnable){ this.priority = priority; this.runnable = runnable; } @Override public void run() { if (runnable != null) { runnable.run(); } } /** * 線程隊(duì)列方式 先進(jìn)先出 * @param r1 * @param r2 * @return */ public static final int compareFIFO(PRunnable r1, PRunnable r2) { int result = r1.priority-r2.priority; return result==0?r1.serial-r2.serial:result; } /** * 線程隊(duì)列方式 后進(jìn)先出 * @param r1 * @param r2 * @return */ public static final int compareLIFO(PRunnable r1, PRunnable r2) { int result = r1.priority-r2.priority; return result==0?r2.serial-r1.serial:result; } } //線程池實(shí)現(xiàn) public class TaskExecutor implements Executor { private final static int QUEUE_INIT_CAPACITY = 20; private static final int CORE = 3; private static final int MAX = 5; private static final int TIMEOUT = 30 * 1000; private AtomicInteger SERIAL = new AtomicInteger(0);//主要獲取添加任務(wù) public static class Config { public int core; public int max; public int timeout; public boolean allowCoreTimeOut; public boolean fifo; public Config(int core, int max, int timeout, boolean allowCoreTimeOut,boolean fifo) { this.core = core; this.max = max; this.timeout = timeout; this.allowCoreTimeOut = allowCoreTimeOut; this.fifo = fifo; } } public static Config defaultConfig = new Config(CORE, MAX, TIMEOUT, true,true); private final String name; private final Config config; private ExecutorService service; public TaskExecutor(String name) { this(name, defaultConfig); } public TaskExecutor(String name, Config config) { this(name, config, true); } public TaskExecutor(String name, Config config, boolean startup) { this.name = name; this.config = config; if (startup) { startup(); } } public void startup() { synchronized (this) { if (service != null && !service.isShutdown()) { return; } service = createExecutor(config); } } public void shutdown() { ExecutorService executor = null; synchronized (this) { // 交換變量 if (service != null) { executor = service; service = null; } } if (executor != null) { // 停止線程 if (!executor.isShutdown()) { executor.shutdown(); } // 回收變量 executor = null; } } private void executeRunnable(PRunnable runnable) { synchronized (this) { if (service == null || service.isShutdown()) { return; } runnable.serial = SERIAL.getAndIncrement(); service.execute(runnable); } } @Override public void execute(Runnable runnable) { if (runnable instanceof PRunnable) { executeRunnable((PRunnable) runnable); }else{ executeRunnable(new PRunnable(runnable)); } } public Future<?> submit(Runnable runnable) { synchronized (this) { if (service == null || service.isShutdown()) { return null; } if (runnable instanceof PRunnable) { ((PRunnable) runnable).serial = SERIAL.getAndIncrement(); return service.submit(runnable); }else{ PRunnable pRunnable = new PRunnable(runnable); pRunnable.serial = SERIAL.getAndIncrement(); return service.submit(pRunnable); } } } public void execute(Runnable runnable, @PRunnable.Priority int priority) { executeRunnable(new PRunnable(priority,runnable)); } private ExecutorService createExecutor(Config config) { ThreadPoolExecutor service = new ThreadPoolExecutor(config.core, config.max, config.timeout, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(QUEUE_INIT_CAPACITY, config.fifo ? mQueueFIFOComparator : mQueueLIFOComparator), new TaskThreadFactory(name), new ThreadPoolExecutor.DiscardPolicy()); allowCoreThreadTimeOut(service, config.allowCoreTimeOut); return service; } public boolean isBusy() { synchronized (this) { if (service == null || service.isShutdown()) { return false; } if(service instanceof ThreadPoolExecutor){ ThreadPoolExecutor tService = (ThreadPoolExecutor) service; return tService.getActiveCount() >= tService.getCorePoolSize(); } return false; } } private static final void allowCoreThreadTimeOut(ThreadPoolExecutor service, boolean value) { if (Build.VERSION.SDK_INT >= 9) { allowCoreThreadTimeOut9(service, value); } } @TargetApi(9) private static final void allowCoreThreadTimeOut9(ThreadPoolExecutor service, boolean value) { service.allowCoreThreadTimeOut(value); } Comparator<Runnable> mQueueFIFOComparator = new Comparator<Runnable>() { @Override public int compare(Runnable lhs, Runnable rhs) { PRunnable r1 = (PRunnable) lhs; PRunnable r2 = (PRunnable) rhs; return PRunnable.compareFIFO(r1, r2); } }; Comparator<Runnable> mQueueLIFOComparator = new Comparator<Runnable>() { @Override public int compare(Runnable lhs, Runnable rhs) { PRunnable r1 = (PRunnable) lhs; PRunnable r2 = (PRunnable) rhs; return PRunnable.compareLIFO(r1, r2); } }; }
以上所述是小編給大家介紹的Android開發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線程與線程池)詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android使用Notification實(shí)現(xiàn)普通通知欄(一)
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)普通通知欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Kotlin基礎(chǔ)學(xué)習(xí)之Deprecated與Suppress注解使用
這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之Deprecated與Suppress注解使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁
這篇文章主要為大家詳細(xì)介紹了使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android App中實(shí)現(xiàn)圖片異步加載的實(shí)例分享
這篇文章主要介紹了Android App中實(shí)現(xiàn)圖片異步加載的實(shí)例分享,這樣GridView在加載大量圖片時(shí)便可以延時(shí)分布顯示,需要的朋友可以參考下2016-04-04Android通過startService實(shí)現(xiàn)文件批量下載
這篇文章主要為大家詳細(xì)介紹了Android通過startService實(shí)現(xiàn)文件批量下載的示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法,涉及Android中Listview的響應(yīng)點(diǎn)擊與樣式變換相關(guān)操作技巧,需要的朋友可以參考下2015-12-12Android自定義View實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11利用Android從0到1實(shí)現(xiàn)一個(gè)流布局控件
新項(xiàng)目用到了一種全新布局,Android標(biāo)簽流式布局的功能,正好一直說給大家講自己定義控件的實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于利用Android從0到1如何實(shí)現(xiàn)一個(gè)流布局控件的相關(guān)資料,需要的朋友可以參考下2021-08-08