問題描述:計算一批正整數(shù)中質(zhì)數(shù)的個數(shù)
使用方法:
使用CompletableFuture結(jié)合線程池 多線程統(tǒng)計質(zhì)數(shù)的個數(shù),最后匯總各個線程的數(shù)量。代碼如下:
// 創(chuàng)建一個固定大小的線程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
int count = getPrimeNumberCount(2, 100000);
System.out.println(Thread.currentThread().getName() + ", count = " + count);
return count;
}, executorService);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
int count = getPrimeNumberCount(100000, 200000);
System.out.println(Thread.currentThread().getName() + ", count = " + count);
return count;
}, executorService);
System.out.println(future1.join() + future2.join());