最近在項目使用Java8 的CompletableFuture執行一些異步多線程任務,一時疏忽,導致ArrayList出現線程安全問題
就算在方法內使用局部變量,但使用異步多線程執行任務,還是會出現線程安全問題
以下是錯誤、正確使用的示例方法:
package test;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.stream.IntStream;
/**
* @version v1.0
* @description:
* @author: 47Gamer on 2020.9.16.016 上午 10:24
*/
public class T {
private static final int SIZE = 1000000;
public static void main(String[] args) {
testCompletableFuture();
testCompletableFuture2();
testCompletableFuture3();
}
/**
* 非線程安全,有時出現數組下標越界問題
*/
public static void testCompletableFuture() {
System.out.println("----start----" + LocalDateTime.now());
//由於方法后面的add操作,這里的變量相當於全局變量,造成了線程安全問題出現
List<Integer> list = new ArrayList<>();
List<CompletableFuture<Void>> futureList = new ArrayList<>();
IntStream.range(0, SIZE).forEach(i -> {
//設置隨機返回數字
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
Random random = new Random();
return random.nextInt(Integer.MAX_VALUE);
}).thenAccept(list::add); //添加到list,出現線程安全問題
futureList.add(future);
});
//主線程阻塞等待所有異步線程完成任務
futureList.forEach(CompletableFuture::join);
System.out.println("testCompletableFuture - size :" + list.size());
System.out.println("----end----" + LocalDateTime.now());
System.out.println();
}
/**
* 線程安全
* 方法一:使用CopyOnWriteArrayList(合適讀多寫少的情況)替換ArrayList
* 方法二:Collections.synchronizedList設置為線程安全類(這里測試比CopyOnWriteArrayList快)
*/
public static void testCompletableFuture2() {
System.out.println("----start----" + LocalDateTime.now());
List<Integer> list = Collections.synchronizedList(new ArrayList<>());
//List<Integer> list = new CopyOnWriteArrayList<>();
List<CompletableFuture<Void>> futureList = new ArrayList<>();
IntStream.range(0, SIZE).forEach(i -> {
//設置隨機返回數字
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
Random random = new Random();
return random.nextInt(Integer.MAX_VALUE);
}).thenAccept(list::add); //添加到list,不會出現線程安全問題
futureList.add(future);
});
//主線程阻塞等待所有異步線程完成任務
futureList.forEach(CompletableFuture::join);
System.out.println("testCompletableFuture2 - size : " + list.size());
System.out.println("----end----" + LocalDateTime.now());
System.out.println();
}
/**
* 線程安全
* 使用join方法再添加到ArrayList
*/
public static void testCompletableFuture3() {
System.out.println("----start----" + LocalDateTime.now());
List<Integer> list = new ArrayList<>();
List<CompletableFuture<Integer>> futureList = new ArrayList<>();
IntStream.range(0, SIZE).forEach(i -> {
//設置隨機返回數字
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
Random random = new Random();
return random.nextInt(Integer.MAX_VALUE);
});
futureList.add(future);
});
//主線程阻塞等待所有異步線程完成任務,並在join返回結果再添加到ArrayList,就不會出現線程安全問題
futureList.forEach(future-> list.add(future.join()));
System.out.println("testCompletableFuture3 - size : " + list.size());
System.out.println("----end----" + LocalDateTime.now());
}
}
