1.背景
最近在处理业务过程中存在给数组中的实体类分别做校验,并且互不干扰,串行执行耗时过大,通过使用并行流提高接口效率。当存在批量获取数据库中的数据而获取的处理集合过大时进行分批处理使用com.google.common.collect提供的Lists工具类快速优雅分批处理。
2.小技巧
2.1 parallelStream基本使用
@Test
public void testC() {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("张三", "男",22));
students.add(new Student("李四", "女",23));
students.add(new Student("王五", "男",26));
students.parallelStream().forEach(student -> {
student.setAge(student.getAge()+1);
System.out.println(student);
});
}
并行流使用过程中往往不是顺序执行,当集合中需要处理的数据直接相互影响,执行有顺序使用并行流会存在问题。另外在CPU资源紧张的时候,使用并行流可能会带来频繁的线程上下文切换,导致并行流执行的效率还没有串行执行的效率高。
2.2 Lists.partition 使用
使用依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
使用模版
@Test
public void testD() {
// 批处理大小
Integer batchSize = 3;
// 需要分批的数组
List<Integer> numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
// 批处理分组数组
Lists.partition(numbers,batchSize).forEach(batchList->{
System.out.println("当前批次:" + batchList);
});
}
3.个人体会
之前才进行一些操作的时候就像上面的两个方法,就自己造轮子,其实有很多的方法都已经封装好了自己写的不但冗余而且可能存在很多问题,当遇到一些问题可以写问一下GPT有没有什么更好的处理方法防止重复造轮子的情况,提高开发效率。