JAVA多線程處理for循環(huán)數(shù)據(jù)詳細(xì)講解
1.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程與子線程無先后順序
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 5; i++) {
ThreadUtil.execAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程" + Thread.currentThread().getName() + "執(zhí)行完");
});
System.out.println("第" + i + "個(gè)線程");
}
System.out.println("完成");
}執(zhí)行結(jié)果:

2.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程在所有子線程執(zhí)行完成之后執(zhí)行
public static void main(String[] args) throws InterruptedException {
//初始化線程數(shù)量
CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(5);
for (int i = 0; i < 5; i++) {
ThreadUtil.execute(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程" + Thread.currentThread().getName() + "執(zhí)行完");
//調(diào)用線程計(jì)數(shù)器-1
countDownLatch.countDown();
});
System.out.println("第" + i + "個(gè)線程");
}
//喚醒主線程
countDownLatch.await();
System.out.println("完成");
}執(zhí)行結(jié)果:

3.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程在所有子線程執(zhí)行完成之后執(zhí)行
public static void main(String[] args) throws InterruptedException {
// 線程個(gè)數(shù)
int N = 10;
// 實(shí)例化一個(gè)倒計(jì)數(shù)器,N指定計(jì)數(shù)個(gè)數(shù)
CountDownLatch countDownLatch = new CountDownLatch(N);
for (int i = 0; i < N; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
System.out.println("子線程" + Thread.currentThread().getName() + "休眠結(jié)束");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 計(jì)數(shù)減一
countDownLatch.countDown();
}
}
}).start();
}
// 阻塞,等待當(dāng)計(jì)數(shù)減到0時(shí),執(zhí)行后面的代碼
countDownLatch.await();
System.out.println("結(jié)束");
}執(zhí)行結(jié)果:

4. JAVA多線程10個(gè)線程處理1000個(gè)數(shù)據(jù)
public static void main(String[] args) throws Exception {
List<Integer> idList = new ArrayList<>();
for (int i = 1; i <= 1000; i++) {
idList.add(i);
}
int threadNum = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
CountDownLatch countDownLatch = new CountDownLatch(threadNum);
int perSize = idList.size() / threadNum;
// 定義接受數(shù)據(jù)集合 多線程情況下,使用線程安全集合
List<Integer> resultList = Collections.synchronizedList(new ArrayList());
for (int i = 0; i < threadNum; i++) {
MultiThread thread = new MultiThread();
thread.setIdList(idList.subList(i * perSize, (i + 1) * perSize));
thread.setCountDownLatch(countDownLatch);
thread.setResultList(resultList);
executorService.submit(thread);
}
countDownLatch.await();
executorService.shutdown();
// 查看結(jié)果
System.out.println(resultList.size());
System.out.println(resultList.stream().sorted().collect(Collectors.toList()));
}
}
class MultiThread extends Thread {
private List<Integer> idList;
private CountDownLatch countDownLatch;
private List<Integer> result;
public void setResultList(List<Integer> result) {
this.result = result;
}
public void setIdList(List<Integer> idList) {
this.idList = idList;
}
public void setCountDownLatch(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
// 數(shù)據(jù)處理
for (Integer integer : idList) {
if (integer % 2 == 0) {
result.add(integer);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (countDownLatch != null) {
countDownLatch.countDown();
}
}
}執(zhí)行結(jié)果:

總結(jié)
到此這篇關(guān)于JAVA多線程處理for循環(huán)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)JAVA處理for循環(huán)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程
這篇文章主要介紹了2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Spring使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對象
這篇文章主要為大家詳細(xì)介紹了Spring如何使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對象,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答
這篇文章主要介紹了教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Java微信公眾平臺開發(fā)(11) 微信三大平臺的關(guān)聯(lián)
這篇文章主要介紹了Java微信公眾平臺開發(fā)第十一步,微信開發(fā)中微信公眾平臺、開放平臺和商戶平臺的關(guān)聯(lián),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Java 使用Filter實(shí)現(xiàn)用戶自動登陸
這篇文章主要介紹了Java 使用Filter實(shí)現(xiàn)用戶自動登陸的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-05-05

