public class App {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        System.out.println(list.size());

        List<Integer> list1 = new ArrayList<>();
        list.stream().forEach(x -> list1.add(x));
        System.out.println(list1.size());

//        List<Integer> list2 = new ArrayList<>();
//        list.parallelStream().forEach(x -> list2.add(x));
//        System.out.println(list2.size());

        List<Integer> list3 = list.parallelStream().collect(Collectors.toList());
        System.out.println(list3.size());
    }
}  

并行Stream因为考虑效率问题,所以没有在意线程安全

我们可以使用线程安全的数据流处理使数据同步

相关文章:

  • 2021-06-24
  • 2022-01-18
  • 2021-06-26
  • 2021-08-06
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-02
  • 2021-12-03
  • 2021-10-19
  • 2021-10-20
  • 2022-12-23
  • 2021-06-23
相关资源
相似解决方案