Java并發(fā)之串行線程池實(shí)例解析
前言
做Android的這兩年時(shí)間,通過研究Android源碼,也會(huì)Java并發(fā)處理多線程有了自己的一些理解。
那么問題來了,如何實(shí)現(xiàn)一個(gè)串行的線程池呢?
思路
何為串行線程池呢?
也就是說,我們的Runnable對(duì)象應(yīng)該有個(gè)排隊(duì)的機(jī)制,它們順序從隊(duì)列尾部進(jìn)入,并且從隊(duì)列頭部選擇Runnable進(jìn)行執(zhí)行。
既然我們有了思路,那我們就考慮一下所需要的數(shù)據(jù)結(jié)構(gòu)?
既然是從隊(duì)列尾部插入Runnable對(duì)象,從隊(duì)列頭部執(zhí)行Runnable對(duì)象,我們自然需要一個(gè)隊(duì)列。Java的SDK已經(jīng)給我們提供了很好的隊(duì)列數(shù)據(jù)結(jié)構(gòu),例如雙端隊(duì)列:ArrayDeque<Runnable>。
- 因?yàn)樯婕暗骄€程的執(zhí)行,那我們首先就需要有一個(gè)合適的線程池,使用ThreadPoolExecutor類即可構(gòu)造。
- 既然是串行執(zhí)行,那如何保持串行機(jī)制呢?我們可以通過try和finally機(jī)制,我們將傳入的Runnable對(duì)象重新封裝成一個(gè)新的Runnable對(duì)象,在新的Runnable的run方法的try塊中執(zhí)行Runnable的run方法,在finally中調(diào)用執(zhí)行隊(duì)列頭部Runnable對(duì)象出隊(duì)列,并放入線程池執(zhí)行的方法。
示例代碼
import java.util.ArrayDeque; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** * Created by wzy on 16-1-5. */ public class SerialExecutor { private Runnable mActive; private ArrayDeque<Runnable> mArrayDeque = new ArrayDeque<>(); private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CORE_POOL_SIZE = CPU_COUNT + 1; private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; private static final int KEEP_ALIVE = 1; private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingDeque<>(128); private static final ThreadFactory sThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); @Override public Thread newThread(Runnable r) { return new Thread(r, "Serial thread #" + mCount.getAndIncrement()); } }; private static final ThreadPoolExecutor THREAD_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory); public synchronized void execute(final Runnable r) { mArrayDeque.offer(new Runnable() { @Override public void run() { try { r.run(); } finally { scheduleNext(); } } }); // 第一次入隊(duì)列時(shí)mActivie為空,因此需要手動(dòng)調(diào)用scheduleNext方法 if (mActive == null) { scheduleNext(); } } private void scheduleNext() { if ((mActive = mArrayDeque.poll()) != null) { THREAD_EXECUTOR.execute(mActive); } } public static void main(String[] args) { SerialExecutor serialExecutor = new SerialExecutor(); for (int i = 0; i < 10; i ++) { final int j = i; serialExecutor.execute(new Runnable() { @Override public void run() { System.out.println("The num is :" + (j + 1)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
執(zhí)行結(jié)果如下:
The num is :1
The num is :2
The num is :3
The num is :4
The num is :5
The num is :6
The num is :7
The num is :8
The num is :9
The num is :10
總結(jié)
以上就是本文關(guān)于Java并發(fā)之串行線程池實(shí)例解析的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
SpringBoot配置@Configuration注解和@bean注解
這篇文章主要介紹了SpringBoot配置@Configuration注解和@bean注解,文章圍繞主題相關(guān)內(nèi)容展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04java.lang.IncompatibleClassChangeError異常的問題解決
本文主要介紹了java.lang.IncompatibleClassChangeError異常的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06SpringBoot工程下Lombok的應(yīng)用教程詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot工程下Lombok應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11解決Java項(xiàng)目啟動(dòng)報(bào)錯(cuò):Logback?configuration?error?detected:問題
這篇文章主要介紹了解決Java項(xiàng)目啟動(dòng)報(bào)錯(cuò):Logback?configuration?error?detected:問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04application.yaml與bootstrap.yaml的使用
這篇文章主要介紹了application.yaml與bootstrap.yaml的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08詳解Mybatis-plus(MP)中CRUD操作保姆級(jí)筆記
本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級(jí)筆記,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11