Java多線程按指定順序同步執(zhí)行
筆者今天看到一個(gè)有趣的面試題,如何讓多個(gè)線程按照既定的順序依次執(zhí)行?比如每個(gè)線程輸出一個(gè)整數(shù),
那么期望就是這樣的:0,1,2,3,4,5,6,7,8,9. 而不是0,2,4,1,3,5,8,7,9,6
乍一看,這不是反人性的考題嗎?多線程本來就以亂序執(zhí)行出名的。稍加思索,想到3種解決方案,分別用代碼實(shí)現(xiàn)之。
方法1 使用newSingleThreadExecutor
newSingleThreadExecutor返回僅僅包含一個(gè)線程的線程池,將多個(gè)任務(wù)交給此Executor時(shí),這個(gè)線程池處理完一個(gè)任務(wù)后接著處理下一個(gè)任務(wù),這樣就保證了執(zhí)行順序,先提交先執(zhí)行。如果當(dāng)前線程意外終止,會創(chuàng)建一個(gè)新線程繼續(xù)執(zhí)行任務(wù)。
示例代碼如下:
ExecutorService pool = Executors.newSingleThreadExecutor();
for(int i=0;i<1000;++i) {
final int number = i;
pool.execute(()-> {
System.out.println("I am " + number);
} );
}
pool.shutdown();
方法2 使用join方法
When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.
英語原版其實(shí)很拗口,不好理解。簡單點(diǎn)說,就是某個(gè)線程A調(diào)用join,其他線程就要乖乖等A執(zhí)行完畢才能執(zhí)行。
示例代碼如下:
public class Worker implements Runnable {
private int number;
public Worker(int i) {
number = i;
}
@Override
public synchronized void run() {
System.out.println("I am " + number);
}
}
public class TestWorker {
public static void main(String[] args) {
for(int j=0;j<1000;++j) {
Thread thread = new Thread(new Worker(j));
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
方法3 使用ThreadPoolExecutor,設(shè)置它的核心線程數(shù)為1
我們先分析一下ThreadPoolExecutor,其構(gòu)造函數(shù)如下
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
各個(gè)參數(shù)含義如下:
1、corePoolSize, 核心線程數(shù),建議和cpu的核心數(shù)一樣,當(dāng)有任務(wù)提交,檢測當(dāng)前線程池內(nèi)的線程數(shù)小于corePoolSize的話,新建線程執(zhí)行任務(wù),直到達(dá)到corePoolSize。線程池內(nèi)的線程數(shù)大于等于corePoolSize時(shí),將任務(wù)放入workQueue等待。
2、maximumPoolSize,允許線程池內(nèi)最大線程數(shù),當(dāng)隊(duì)列滿了之后,如果線程池內(nèi)的線程數(shù)小于maximumPoolSize新建線程,如果大于等于執(zhí)行拒絕策略。
3、keepAliveTime,線程最大空閑時(shí)間,如果設(shè)置60s,那么線程空閑60s后自動(dòng)結(jié)束。
unit,時(shí)間單位分鐘,秒等等。
4、workQueue,線程數(shù)超過corePoolSize存放任務(wù)的地方。
5、threadFactory,線程工廠,默認(rèn)的即可。
6、handler,拒絕策略,分4種,AbortPolicy直接拋出異常、DiscardPolicy悄悄拋棄不執(zhí)行、CallerRunsPolicy(調(diào)用者運(yùn)行):該策略既不會拋棄任務(wù)也不會拋出異常,而是將這個(gè)任務(wù)退回給調(diào)用者,從而降低新任務(wù)的流量;、DiscardOldestPolicy(拋棄最舊的)
示例代碼如下:
ExecutorService pool = new ThreadPoolExecutor(1, 1000, 300, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(1000),Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
for(int i=0;i<1000;++i) {
final int number = i;
pool.execute(()-> {
System.out.println("I am " + number);
} );
}
pool.shutdown();
4. 執(zhí)行結(jié)果
I am 0 I am 1 I am 2 I am 3 I am 4 I am 5 I am 6 I am 7 I am 8 I am 9 I am 10 。。。 I am 990 I am 991 I am 992 I am 993 I am 994 I am 995 I am 996 I am 997 I am 998 I am 999
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java全角與半角標(biāo)點(diǎn)符號相互轉(zhuǎn)換詳解
這篇文章主要為大家介紹了java全角與半角標(biāo)點(diǎn)符號相互轉(zhuǎn)換詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn)
在我們的實(shí)際業(yè)務(wù)中可能會遇到;在一個(gè)項(xiàng)目里面讀取多個(gè)數(shù)據(jù)庫的數(shù)據(jù)來進(jìn)行展示,spring對同時(shí)配置多個(gè)數(shù)據(jù)源是支持的,本文主要介紹了springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn),感興趣的可以了解一下2022-03-03
java實(shí)體類轉(zhuǎn)json時(shí)null值不要轉(zhuǎn)為"null"問題
這篇文章主要介紹了java實(shí)體類轉(zhuǎn)json時(shí)null值不要轉(zhuǎn)為“null”問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java Spring Cloud Bus 實(shí)現(xiàn)配置實(shí)時(shí)更新詳解
這篇文章主要介紹了SpringCloud Bus如何實(shí)現(xiàn)配置刷新,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09
SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式
這篇文章主要介紹了SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
idea激活A(yù)ctivateJrebel熱部署的方法詳解
這篇文章主要介紹了idea激活A(yù)ctivateJrebel熱部署的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java中String類getBytes()方法詳解與完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Java中String類getBytes()方法詳解與完整實(shí)例的相關(guān)資料,getBytes()是Java編程語言中將一個(gè)字符串轉(zhuǎn)化為一個(gè)字節(jié)數(shù)組byte[]的方法,需要的朋友可以參考下2023-10-10

