随记:CompletableFuture + ThreadPoolExecutor 实现异步批量插入

该文章介绍了如何通过CompletableFuture配合ThreadPoolExecutor实现异步方式,模拟批量向数据库插入User对象,以提高性能并控制并发量。
摘要由CSDN通过智能技术生成

实现:通过 CompletableFuture + ThreadPoolExecutor : 异步编辑器 + 自定义线程池实现异步批量插入

场景:模拟批量向数据库中插入 User 信息。

主要对象:User(id, name)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 利用 CompletableFutrue + ThreadPoolExecutor 实现异步批量处理数据
 * 
 * 注:控制台数据很耗时。建议只输出线程名
 */
public class CompletableFutrue {
    public static void main(String[] args) {
        // 1.创建异步 list 集合与自定义线程池
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            20, 50, 10000, TimeUnit.MILLISECONDS, 
            new ArrayBlockingQueue<>(10000));

        int saveBatch = 5000;
        int count = 1;

        // 模拟方法在逻辑层应该是:userService
        Person temPerson = new Person();
        long start = System.currentTimeMillis();
        // 20 批次
        for (int i = 0; i < 20; i++) {
            ArrayList<Person> persons = new ArrayList<>();
            // 每批次
            while (true) {
                Person person = new Person();
                person.setId(count++);
                person.setName("name");

                persons.add(person);

                // 达到批次弹出
                if (count % saveBatch == 0) {
                    break;
                }
            }

            // 2.异步执行,构建批次执行对象
            CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
                System.out.println("进入批次处理:" + Thread.currentThread().getName());
                temPerson.aaa(persons);
            }, executor);

            // 添加到线程集合
            futures.add(future);
        }

        // 3.将futures集合中的元素转为串行化执行
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join();

        long end = System.currentTimeMillis();

        System.out.println("耗时:" + (end - start) + "ms");
        
    }


}

class Person {

    private int id;
    private String name;

    public void aaa(List<Person> list) {
        // 假设;userService.saveBatch()
        Collections.reverse(list);
    }

    // 构造方法
    public Person() {         
    }        
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // setter getter 
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
    @Override
    public String toString() {
        return "Person( " +
            "id=" + this.id +
            ", name=" + this.name +
            ")";
    }
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值