欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java多线程批量数据导入的方法详解

程序员文章站 2022-10-13 23:46:35
前言: 当遇到大量数据导入时,为了提高处理的速度,可以选择使用多线程来批量处理这些处理。常见的场景有: 大文件导入数据库(这个文件不一定是标准的csv可导入文件...

前言:

当遇到大量数据导入时,为了提高处理的速度,可以选择使用多线程来批量处理这些处理。常见的场景有:

  • 大文件导入数据库(这个文件不一定是标准的csv可导入文件或者需要在内存中经过一定的处理)
  • 数据同步(从第三方接口拉取数据处理后写入自己的数据库)

以上的场景有一个共性,这类数据导入的场景简单来说就是将数据从一个数据源移动到另外一个数据源,而其中必定可以分为两步

  • 数据读取:从数据源读取数据到内存
  • 数据写入:将内存中的数据写入到另外一个数据源,可能存在数据处理

而且根据读取的速度一般会比数据写入的速度快很多,即读取快,写入慢。

设计思路

由于场景的特点是读取快,写入慢,如果是使用多线程处理,建议是数据写入部分改造为多线程。而数据读取可以改造成批量读取数据。简单来说就是两个要点:

  • 批量读取数据
  • 多线程写入数据

示例

多线程批量处理最简单的方案是使用线程池来进行处理,下面会通过一个模拟批量读取和写入的服务,以及对这个服务的多线程写入调用作为示例,展示如何多线程批量数据导入。

模拟服务

import java.util.concurrent.atomic.atomiclong;
/**
* 数据批量写入用的模拟服务
*
* @author rjh
* create at 2019-04-01
*/
public class mockservice {
/**
* 可读取总数
*/
private long canreadtotal;
/**
* 写入总数
*/
private atomiclong writetotal=new atomiclong(0);
/**
* 写入休眠时间(单位:毫秒)
*/
private final long sleeptime;
/**
* 构造方法
*
* @param canreadtotal
* @param sleeptime
*/
public mockservice(long canreadtotal, long sleeptime) {
this.canreadtotal = canreadtotal;
this.sleeptime = sleeptime;
}
/**
* 批量读取数据接口
*
* @param num
* @return
*/
public synchronized long readdata(int num) {
long readnum;
if (canreadtotal >= num) {
canreadtotal -= num;
readnum = num;
} else {
readnum = canreadtotal;
canreadtotal = 0;
}
//system.out.println("read data size:" + readnum);
return readnum;
}
/**
* 写入数据接口
*/
public void writedata() {
try {
// 休眠一定时间模拟写入速度慢
thread.sleep(sleeptime);
} catch (interruptedexception e) {
e.printstacktrace();
}
// 写入总数自增
system.out.println("thread:" + thread.currentthread() + " write data:" + writetotal.incrementandget());
}
/**
* 获取写入的总数
*
* @return
*/
public long getwritetotal() {
return writetotal.get();
}
}

批量数据处理器

import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
/**
* 基于线程池的多线程批量写入处理器
* @author rjh
* create at 2019-04-01
*/
public class simplebatchhandler {
private executorservice executorservice;
private mockservice service;
/**
* 每次批量读取的数据量
*/
private int batch;
/**
* 线程个数
*/
private int threadnum;
public simplebatchhandler(mockservice service, int batch,int threadnum) {
this.service = service;
this.batch = batch;
//使用固定数目的线程池
this.executorservice = executors.newfixedthreadpool(threadnum);
}
/**
* 开始处理
*/
public void starthandle() {
// 开始处理的时间
long starttime = system.currenttimemillis();
system.out.println("start handle time:" + starttime);
long readdata;
while ((readdata = service.readdata(batch)) != 0) {// 批量读取数据,知道读取不到数据才停止
for (long i = 0; i < readdata; i++) {
executorservice.execute(() -> service.writedata());
}
}
// 关闭线程池
executorservice.shutdown();
while (!executorservice.isterminated()) {//等待线程池中的线程执行完
}
// 结束时间
long endtime = system.currenttimemillis();
system.out.println("end handle time:" + endtime);
// 总耗时
system.out.println("total handle time:" + (endtime - starttime) + "ms");
// 写入总数
system.out.println("total write num:" + service.getwritetotal());
}
}

测试类

/**
* simplebatchhandler的测试类
* @author rjh
* create at 2019-04-01
*/
public class simplebatchhandlertest {
public static void main(string[] args) {
// 总数
long total=100000;
// 休眠时间
long sleeptime=100;
// 每次拉取的数量
int batch=100;
// 线程个数
int threadnum=16;
mockservice mockservice=new mockservice(total,sleeptime);
simplebatchhandler handler=new simplebatchhandler(mockservice,batch,threadnum);
handler.starthandle();
}
}

运行结果

start handle time:1554298681755
thread:thread[pool-1-thread-2,5,main] write data:1
thread:thread[pool-1-thread-1,5,main] write data:2
...省略部分输出
thread:thread[pool-1-thread-4,5,main] write data:100000
end handle time:1554299330202
total handle time:648447ms
total write num:100000

分析

在单线程情况下的执行时间应该为total*sleeptime,即10000000ms,而改造为多线程后执行时间为648447ms。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。