java利用CountDownLatch實(shí)現(xiàn)并行計(jì)算
本文實(shí)例為大家分享了利用CountDownLatch實(shí)現(xiàn)并行計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下
import java.util.concurrent.CountDownLatch;
/**
* @Author pipi
* @Date 2018/10/15 13:56
**/
public class ParallelComputing {
private int[] nums;
private String[] info;
private CountDownLatch countDownLatch;
public ParallelComputing(String[] info) {
this.info = info;
int size = info.length;
nums = new int[size];
this.countDownLatch = new CountDownLatch(size);
}
public void calc(String line, int index) throws InterruptedException {
String[] numbers = line.split(",");
int total = 0;
for (String num : numbers) {
total += Integer.parseInt(num);
}
Thread.sleep(5000);
nums[index] = total;
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName() + "執(zhí)行計(jì)算任務(wù)..." + line + ",結(jié)果為:" + total);
}
public void sum() {
System.out.println("匯總線程開始執(zhí)行...");
int total = 0;
for (int i : nums) {
total += i;
}
System.out.println("匯總線程結(jié)束執(zhí)行...結(jié)果為:" + total);
}
public void calcSum() throws InterruptedException {
int size = info.length;
for (int i = 0; i < size; i++) {
final int j = i;
new Thread(() -> {
try {
calc(info[j], j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
countDownLatch.await();
sum();
}
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
String[] info = {
"2,22",
"3,33",
"232,32,76,84",
"99,45,1"
};
ParallelComputing parallelComputing = new ParallelComputing(info);
parallelComputing.calcSum();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java騷操作之CountDownLatch代碼詳解
- java多線程CountDownLatch與線程池ThreadPoolExecutor/ExecutorService案例
- java并發(fā)編程專題(八)----(JUC)實(shí)例講解CountDownLatch
- 詳解java CountDownLatch和CyclicBarrier在內(nèi)部實(shí)現(xiàn)和場(chǎng)景上的區(qū)別
- Java線程并發(fā)工具類CountDownLatch原理及用法
- JAVA CountDownLatch(倒計(jì)時(shí)計(jì)數(shù)器)用法實(shí)例
- JAVA CountDownLatch與thread-join()的區(qū)別解析
- 淺談java并發(fā)之計(jì)數(shù)器CountDownLatch
- JAVA多線程CountDownLatch使用詳解
- Java并發(fā)編程之CountDownLatch源碼解析
相關(guān)文章
解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
使用spring?data的page和pageable如何實(shí)現(xiàn)分頁(yè)查詢
這篇文章主要介紹了使用spring?data的page和pageable如何實(shí)現(xiàn)分頁(yè)查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java動(dòng)態(tài)設(shè)置注解值及原理詳解
這篇文章主要介紹了Java動(dòng)態(tài)設(shè)置注解值及原理詳解,AnnotationInvocationHandler是注解的代理hander,通過(guò)反射獲取類的注解時(shí)會(huì)通過(guò)AnnotationInvocationHandler創(chuàng)建代理對(duì)象并將數(shù)據(jù)存儲(chǔ)到memberValues里,需要的朋友可以參考下2023-11-11
Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能,結(jié)合實(shí)例形式分析了Java結(jié)合JFrame框架的計(jì)時(shí)器功能相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
java實(shí)現(xiàn)輕輕松松控制臺(tái)斗地主的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)輕輕松松控制臺(tái)斗地主,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
mybatis-plus分頁(yè)查詢?nèi)N方法小結(jié)
本文主要介紹了mybatis-plus分頁(yè)查詢?nèi)N方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

