java并发执行批量插入
1、mybatis-plus批量插入
-
long start = System.currentTimeMillis();int num = 5000; //一次批量插入的数量int j = 0;for (int i = 0;i<20;i++){List<User> userList = new ArrayList<>();while (true){j++;User user = new User();user.setUserProfile("我是假数据");user.setUsername("假数据");user.setAvatarUrl("https://xxxxxx.jpg");user.setGender(0);user.setUserAccount("jiazzj");user.setUserPassword("2970a1691c8ef07f40ddbe6f7b18662f");user.setPhone("123456789");user.setEmail("123456@qq.com");user.setUserStatus(0);user.setUserRole(0);String[] str = new String[]{"java","考研"};Gson gson = new Gson();String json = gson.toJson(str);user.setTags(json);userList.add(user);if (j % num == 0){break;}}userService.saveBatch(userList,num); //mybatis-plus通过传入列表批量插入}long end = System.currentTimeMillis();System.out.println("用时:"+(end - start));
-
用时:6544 ms
2、采用Java并发运行
-
多线程分析
- 这里批量插入数据,是 io 密集任务,我们可以分配比 cpu核心数多的线程并发工作
-
private ExecutorService executorService = new ThreadPoolExecutor(40, 1000, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(2500));
- 分配 40 个线程
- 最大线程数为 1000
- 非核心线程闲置超时时间为 :1分钟
- 任务队列容量为 2500
-
现在MySQL含有20万,准备执行
-
long start = System.currentTimeMillis();int num = 2500;int j = 0;List<CompletableFuture<Void>> futureList = new ArrayList<>();for (int i = 0; i < 40; i++) {List<User> userList = new ArrayList<>();while (true){j++;User user = new User();user.setUserProfile("我是假数据");user.setUsername("假数据");user.setAvatarUrl("https://xxxx.jpg");user.setGender(0);user.setUserAccount("jiazzj");user.setUserPassword("2970a1691c8ef07f40ddbe6f7b18662f");user.setPhone("123456789");user.setEmail("123456@qq.com");user.setUserStatus(0);user.setUserRole(0);String[] str = new String[]{"java","考研"};Gson gson = new Gson();String json = gson.toJson(str);user.setTags(json);userList.add(user);if (j % num == 0){break;}}//定义一个异步任务进行批量插入CompletableFuture<Void> future = CompletableFuture.runAsync(()->{System.out.println("线程名" + Thread.currentThread().getName());userService.saveBatch(userList, num);},executorService);//每个任务执行完,放人list列表里futureList.add(future);}//等待线程全部执行完后执行后续操作CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();long end = System.currentTimeMillis();System.out.println("用时:"+(end - start));
-
用时:1889ms,可以看到这里是40个线程并发执行的任务,提升很显著