溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java8中parallelStream并發(fā)安全的示例分析

發(fā)布時(shí)間:2021-09-09 10:39:11 來源:億速云 閱讀:165 作者:小新 欄目:編程語言

這篇文章主要介紹Java8中parallelStream并發(fā)安全的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

背景

Java8的stream接口極大地減少了for循環(huán)寫法的復(fù)雜性,stream提供了map/reduce/collect等一系列聚合接口,還支持并發(fā)操作:parallelStream。

在爬蟲開發(fā)過程中,經(jīng)常會(huì)遇到遍歷一個(gè)很大的集合做重復(fù)的操作,這時(shí)候如果使用串行執(zhí)行會(huì)相當(dāng)耗時(shí),因此一般會(huì)采用多線程來提速。Java8的paralleStream用fork/join框架提供了并發(fā)執(zhí)行能力。但是如果使用不當(dāng),很容易陷入誤區(qū)。

Java8的paralleStream是線程安全的嗎

一個(gè)簡單的例子,在下面的代碼中采用stream的forEach接口對1-10000進(jìn)行遍歷,分別插入到3個(gè)ArrayList中。其中對第一個(gè)list的插入采用串行遍歷,第二個(gè)使用paralleStream,第三個(gè)使用paralleStream的同時(shí)用ReentryLock對插入列表操作進(jìn)行同步:

private static List<Integer> list1 = new ArrayList<>();
private static List<Integer> list2 = new ArrayList<>();
private static List<Integer> list3 = new ArrayList<>();
private static Lock lock = new ReentrantLock();

public static void main(String[] args) {
 IntStream.range(0, 10000).forEach(list1::add);

 IntStream.range(0, 10000).parallel().forEach(list2::add);

 IntStream.range(0, 10000).forEach(i -> {
 lock.lock();
 try {
  list3.add(i);
 }finally {
  lock.unlock();
 }
 });

 System.out.println("串行執(zhí)行的大?。?quot; + list1.size());
 System.out.println("并行執(zhí)行的大?。?quot; + list2.size());
 System.out.println("加鎖并行執(zhí)行的大?。?quot; + list3.size());
}

執(zhí)行結(jié)果:

串行執(zhí)行的大?。?0000
并行執(zhí)行的大?。?595
加鎖并行執(zhí)行的大小:10000

并且每次的結(jié)果中并行執(zhí)行的大小不一致,而串行和加鎖后的結(jié)果一直都是正確結(jié)果。顯而易見,stream.parallel.forEach()中執(zhí)行的操作并非線程安全。

那么既然paralleStream不是線程安全的,是不是在其中的進(jìn)行的非原子操作都要加鎖呢?我在stackOverflow上找到了答案:

  • https://codereview.stackexchange.com/questions/60401/using-java-8-parallel-streams

  • https://stackoverflow.com/questions/22350288/parallel-streams-collectors-and-thread-safety

在上面兩個(gè)問題的解答中,證實(shí)paralleStream的forEach接口確實(shí)不能保證同步,同時(shí)也提出了解決方案:使用collect和reduce接口。

  • http://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

在Javadoc中也對stream的并發(fā)操作進(jìn)行了相關(guān)介紹:

The Collections Framework provides synchronization wrappers, which add automatic synchronization to an arbitrary collection, making it thread-safe.

Collections框架提供了同步的包裝,使得其中的操作線程安全。

所以下一步,來看看collect接口如何使用。

stream的collect接口

閑話不多說直接上源碼吧,Stream.java中的collect方法句柄:

<R, A> R collect(Collector<? super T, A, R> collector);

在該實(shí)現(xiàn)方法中,參數(shù)是一個(gè)Collector對象,可以使用Collectors類的靜態(tài)方法構(gòu)造Collector對象,比如Collectors.toList(),toSet(),toMap(),etc,這塊很容易查到API故不細(xì)說了。

除此之外,我們?nèi)绻赾ollect接口中做更多的事,就需要自定義實(shí)現(xiàn)Collector接口,需要實(shí)現(xiàn)以下方法:

Supplier<A> supplier();
BiConsumer<A, T> accumulator();
BinaryOperator<A> combiner();
Function<A, R> finisher();
Set<Characteristics> characteristics();

要輕松理解這三個(gè)參數(shù),要先知道fork/join是怎么運(yùn)轉(zhuǎn)的,一圖以蔽之:

Java8中parallelStream并發(fā)安全的示例分析

上圖來自:http://www.infoq.com/cn/articles/fork-join-introduction

簡單地說就是大任務(wù)拆分成小任務(wù),分別用不同線程去完成,然后把結(jié)果合并后返回。所以第一步是拆分,第二步是分開運(yùn)算,第三步是合并。這三個(gè)步驟分別對應(yīng)的就是Collector的supplier,accumulator和combiner。talk is cheap show me the code,下面用一個(gè)例子來說明:

輸入是一個(gè)10個(gè)整型數(shù)字的ArrayList,通過計(jì)算轉(zhuǎn)換成double類型的Set,首先定義一個(gè)計(jì)算組件:

Compute.java:

public class Compute {
public Double compute(int num) {
 return (double) (2 * num);
}
}

接下來在Main.java中定義輸入的類型為ArrayList的nums和類型為Set的輸出結(jié)果result:

private List<Integer> nums = new ArrayList<>();
private Set<Double> result = new HashSet<>();

定義轉(zhuǎn)換list的run方法,實(shí)現(xiàn)Collector接口,調(diào)用內(nèi)部類Container中的方法,其中characteristics()方法返回空set即可:

public void run() {
 // 填充原始數(shù)據(jù),nums中填充0-9 10個(gè)數(shù)
 IntStream.range(0, 10).forEach(nums::add);
 //實(shí)現(xiàn)Collector接口
 result = nums.stream().parallel().collect(new Collector<Integer, Container, Set<Double>>() {

 @Override
 public Supplier<Container> supplier() {
  return Container::new;
 }

 @Override
 public BiConsumer<Container, Integer> accumulator() {
  return Container::accumulate;
 }

 @Override
 public BinaryOperator<Container> combiner() {
  return Container::combine;
 }

 @Override
 public Function<Container, Set<Double>> finisher() {
  return Container::getResult;
 }

 @Override
 public Set<Characteristics> characteristics() {
  // 固定寫法
  return Collections.emptySet();
 }
 });
}

構(gòu)造內(nèi)部類Container,該類的作用是一個(gè)存放輸入的容器,定義了三個(gè)方法:

  • accumulate方法對輸入數(shù)據(jù)進(jìn)行處理并存入本地的結(jié)果

  • combine方法將其他容器的結(jié)果合并到本地的結(jié)果中

  • getResult方法返回本地的結(jié)果

Container.java:

class Container {
 // 定義本地的result
 public Set<Double> set;

 public Container() {
 this.set = new HashSet<>();
 }

 public Container accumulate(int num) {
 this.set.add(compute.compute(num));
 return this;
 }

 public Container combine(Container container) {
 this.set.addAll(container.set);
 return this;
 }

 public Set<Double> getResult() {
 return this.set;
 }
}

在Main.java中編寫測試方法:

public static void main(String[] args) {
 Main main = new Main();
 main.run();
 System.out.println("原始數(shù)據(jù):");
 main.nums.forEach(i -> System.out.print(i + " "));
 System.out.println("\n\ncollect方法加工后的數(shù)據(jù):");
 main.result.forEach(i -> System.out.print(i + " "));
}

輸出:

原始數(shù)據(jù):
0 1 2 3 4 5 6 7 8 9

collect方法加工后的數(shù)據(jù):
0.0 2.0 4.0 8.0 16.0 18.0 10.0 6.0 12.0 14.0

我們將10個(gè)整型數(shù)值的list轉(zhuǎn)成了10個(gè)double類型的set,至此驗(yàn)證成功~

以上是“Java8中parallelStream并發(fā)安全的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI