1. 程式人生 > >單表插入100萬條記錄的多執行緒實現

單表插入100萬條記錄的多執行緒實現

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()));
  }

}