单表插入100万条记录的多线程实现

时间:2022-11-17 04:33:33

public class InsertRecord implements Runnable{

  private int startIndex;
  private int endIndex;
 
  public InsertRecord(int startIndex,int endIndex){
    this.startIndex = startIndex;
    this.endIndex = endIndex;
  }
 
  public void run() {
    TblSchool school = new TblSchool();
    TblSchoolDAO dao = TblSchoolDAO.getInst();
    for(int i = startIndex;i <= endIndex;i++){
      school.setPkSchoolid(i+"");
      school.setColName("dxhtest");
      school.setColRemark("ooo");
      school.setColType(1L);
      dao.save(school);
    }
  }
 

该类为线程类,实现了插入表的记录;

public class Util {
 
  public void insert(){
    int threadCount = 1000;
    int sumRecord = 1000000;//百万
    int step = sumRecord / threadCount;
    Date currentDate = new Date();
    for(int i = 0; i< threadCount;i++){
      Thread thread = new Thread(new InsertRecord(i*step,i*step+step-1));
      thread.start();
    }
    System.out.println("Time is :"+(new Date().getTime()-currentDate.getTime()));
  }

}