android-线程池-最顺手的写法
生活随笔
收集整理的這篇文章主要介紹了
android-线程池-最顺手的写法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
引子
關(guān)于線程池,在這里寫出幾種最順手的寫法,至于原理以及各種細(xì)節(jié)。放后面再填;
經(jīng)過查證,凡是?以前new?Thread()的地方,貌似都可以用線程池來執(zhí)行,優(yōu)化內(nèi)存消耗。
?
代碼
系統(tǒng)提供的4種預(yù)設(shè)線程池類:
1 Runnable runnable = new Runnable() { 2 @Override 3 public void run() { 4 Log.d("atm", "假裝有執(zhí)行過程·"); 5 } 6 }; 7 8 //第一類 9 // 全部由核心線程去實(shí)現(xiàn),并不會(huì)被回收,沒有超時(shí)限制和任務(wù)隊(duì)列的限制,會(huì)創(chuàng)建一個(gè)定長(zhǎng)線程池, 10 // 可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待 11 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4); 12 fixedThreadPool.execute(runnable); 13 14 //第二類 15 //該模式下線程數(shù)量不定的線程池,只有非核心線程,最大值為Integer.MAX_VALUE,會(huì)創(chuàng)建一個(gè)可緩存線程池, 16 // 如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程 17 ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); 18 cachedThreadPool.execute(runnable); 19 20 //第三類 21 //該模式下核心線程是固定的,非核心線程沒有限制,非核心線程閑置時(shí)會(huì)被回收。 22 // 會(huì)創(chuàng)建一個(gè)定長(zhǎng)線程池,執(zhí)行定時(shí)任務(wù)和固定周期的任務(wù) 23 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(4); 24 scheduledThreadPool.schedule(runnable, 2000, TimeUnit.SECONDS);//2000ms后執(zhí)行。 25 scheduledThreadPool.scheduleAtFixedRate(runnable, 10, 1000, TimeUnit.MILLISECONDS);//延遲10ms后,每隔1000ms執(zhí)行一次 26 27 //第四類, 28 //該模式下線程池內(nèi)部只有一個(gè)線程,所有的任務(wù)都在一個(gè)線程中執(zhí)行,會(huì)創(chuàng)建一個(gè)單線程化的線程池, 29 // 它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行 30 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); 31 singleThreadExecutor.execute(runnable);?
不用系統(tǒng)的,而是自己定義各種參數(shù):
1 import android.support.annotation.NonNull; 2 3 import java.util.concurrent.Callable; 4 import java.util.concurrent.Future; 5 import java.util.concurrent.LinkedBlockingDeque; 6 import java.util.concurrent.ThreadFactory; 7 import java.util.concurrent.ThreadPoolExecutor; 8 import java.util.concurrent.TimeUnit; 9 import java.util.concurrent.atomic.AtomicInteger; 10 11 /** 12 */ 13 public class CustomThreadPool { 14 15 private static final int CORE_POOL_SIZE = 3;//核心線程數(shù)目 16 private static final int MAX_POOL_SIZE = 20;//最大線程數(shù),除了核心線程就是非核心線程 17 private static final int ALIVE_TIME = 5;//非核心線程允許閑置的最大時(shí)長(zhǎng) 18 19 private static final CustomThreadPool instance; 20 21 private final ThreadPoolExecutor pool; 22 23 static { 24 instance = new CustomThreadPool(); 25 } 26 27 private CustomThreadPool() { 28 //參數(shù)逐個(gè)解析 29 /** 30 * @param 核心線程的數(shù)目,即使他們是閑置狀態(tài),也不會(huì)被回收,除非你設(shè)置 allowCoreThreadTimeOut,讓核心線程也有超時(shí)時(shí)間(不過一般不這么做) 31 * @param 線程池的最大容量,可以容納核心線程和非核心線程 32 * @param 當(dāng)線程數(shù)目大于核心線程數(shù),這個(gè)值是被回收的最大閑置時(shí)間,超出則會(huì)被回收 33 * @param 超時(shí)時(shí)間的單位(一般用秒,或者毫秒) 34 * @param 等待隊(duì)列,當(dāng)核心線程都在工作,而又有新的任務(wù)需要執(zhí)行,這些任務(wù)則會(huì)先進(jìn)入等待隊(duì)列(但是如果進(jìn)不去,或者隊(duì)列滿了,就會(huì)嘗試用非核心線程) 35 * @param 生成線程的工廠(一般都會(huì)自己new 一個(gè)類繼承ThreadFactory) 36 * @throws IllegalArgumentException 參數(shù)異常 37 * 可能拋出的異常, 38 * 比如,你把核心線程數(shù)設(shè)置為負(fù)數(shù); 39 * 或者超時(shí)時(shí)間設(shè)置為負(fù)數(shù) 40 * 或者最大線程數(shù)是非正數(shù); 41 * 或者最大線程數(shù)小于 核心線程數(shù) 42 * @throws NullPointerException 43 * 當(dāng) 工作隊(duì)列是空,或者 線程工廠,對(duì)象是空,就會(huì)報(bào)空指針 44 */ 45 pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(), new MyThreadFactory()); 46 } 47 48 public static CustomThreadPool getInstance() { 49 return instance; 50 } 51 52 /** 53 * 執(zhí)行,無返回值 54 * 55 * @param r 56 */ 57 public void execute(Runnable r) { 58 pool.execute(r); 59 } 60 61 /** 62 * 提交,有返回值 63 * 64 * @param r 65 * @return 66 */ 67 public Future<String> submit(Runnable r, String s) { 68 return pool.submit(r, s); 69 } 70 71 public Future<String> submit(Callable<String> callable) { 72 return pool.submit(callable); 73 } 74 75 private class MyThreadFactory implements ThreadFactory { 76 77 private final AtomicInteger mCount = new AtomicInteger(1); 78 79 @Override 80 public Thread newThread(@NonNull Runnable r) { 81 Thread thread = new Thread(r, "GWThreadPool-" + mCount.getAndIncrement()); 82 thread.setPriority(Thread.NORM_PRIORITY - 1); 83 thread.setDaemon(false); 84 return thread; 85 } 86 } 87 88 }?
帶返回值的執(zhí)行,以及不帶返回值的執(zhí)行;
1 private void test2() { 2 //如果你想執(zhí)行一個(gè)帶返回值的任務(wù),任務(wù)執(zhí)行完成之后,返回結(jié)果,用下面的代碼 3 try { 4 Callable<String> callable = new Callable<String>() {// 注意這里不是Runnable,而是Callable, 5 @Override 6 public String call() { 7 return "哈哈哈"; 8 } 9 }; 10 11 //````如果你想執(zhí)行任務(wù),并且要取執(zhí)行完成之后的返回值,用submit吧 12 Future<String> s = CustomThreadPool.getInstance().submit(callable);//執(zhí)行,submit 有返回值 13 //下面有5個(gè)API可供調(diào)用 14 /** 15 * 嘗試取消任務(wù)的執(zhí)行。這種嘗試將會(huì)失敗,當(dāng)任務(wù)已經(jīng)完成,已經(jīng)被取消,或者因?yàn)槟撤N原因不能被取消。 16 * 如果成功取消,這個(gè)任務(wù)還沒開始的話,那這個(gè)任務(wù)將永遠(yuǎn)不會(huì)執(zhí)行, 17 * 如果任務(wù)已經(jīng)開始,那就要 mayInterruptIfRunning 參數(shù)值將會(huì)決定是否要嘗試去終止任務(wù);(true,嘗試終止,false,不去嘗試;至于為什么這里是嘗試,而不是一定終止,參照本段開頭;) 18 * 當(dāng)這個(gè)方法返回,后來的調(diào)用isDone將會(huì)永遠(yuǎn)返回true,也就是說,取消也算做是完成? 19 * 后來的調(diào)用isCanceled 將會(huì)永遠(yuǎn)返回true,如果這個(gè)方法返回true的話。 20 * @param mayInterruptIfRunning 是否執(zhí)行這個(gè)任務(wù)線程的線程能夠被中斷;true能夠中斷,false,將會(huì)繼續(xù)執(zhí)行直到完成; 21 * @return 返回值,false,如果這個(gè)任務(wù)不能被取消,典型的就是這個(gè)任務(wù)已經(jīng)完成了·· 其他情況,返回true; 22 */ 23 s.cancel(true);// 取消任務(wù),參數(shù)的意思是:是否允許在執(zhí)行過程中中斷; 如果true,不管是不是已經(jīng)開始任務(wù),都讓他終止;false,如果已經(jīng)開始了,就不終止了; 24 s.isDone();//是否已完成 25 s.isCancelled();// 是否已經(jīng)被取消 26 27 String result = s.get();// 獲取執(zhí)行的結(jié)果,如果任務(wù)尚未執(zhí)行完成,有可能會(huì)阻塞一段時(shí)間 28 String result2 = s.get(3, TimeUnit.SECONDS);//最多等待3秒,get的重載方法,因?yàn)橛锌赡軙?huì)阻塞,阻塞的時(shí)長(zhǎng)不定,所以提供一個(gè)重載方法,指定阻塞的最大時(shí)間; 29 30 //````如果你只是想執(zhí)行任務(wù),不想要返回值,那么,用execute 31 Runnable runnable = new Runnable() { 32 @Override 33 public void run() { 34 Log.d("hahaha", "假裝這里有代碼"); 35 } 36 }; 37 CustomThreadPool.getInstance().execute(runnable); 38 39 } catch (InterruptedException e) { 40 e.printStackTrace(); 41 } catch (ExecutionException e) { 42 e.printStackTrace(); 43 } catch (TimeoutException e) { 44 e.printStackTrace(); 45 } 46 }?
轉(zhuǎn)載于:https://www.cnblogs.com/hankzhouAndroid/p/9505423.html
總結(jié)
以上是生活随笔為你收集整理的android-线程池-最顺手的写法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue.js--基础事件定义,获取数据,
- 下一篇: sqlx使用说明