批量匯入大資料表的時候 使用執行緒池處理
阿新 • • 發佈:2018-12-31
public class UpdateMebinfoThread implements Runnable{
private int beginnum= 0;// 開始行數
private int endnum= 0;// 讀取記錄數
public UpdateMebinfoThread(int beginnum, int endnum)
{
super();
this.beginnum= beginnum;
this.endnum= endnum;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "===beginnum:"+beginnum);
System.out.println(Thread.currentThread().getName() + "====endnum:"+endnum);
}
private int beginnum= 0;// 開始行數
private int endnum= 0;// 讀取記錄數
public UpdateMebinfoThread(int beginnum, int endnum)
{
super();
this.beginnum= beginnum;
this.endnum= endnum;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "===beginnum:"+beginnum);
System.out.println(Thread.currentThread().getName() + "====endnum:"+endnum);
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool= Executors.newScheduledThreadPool(10);// 初始化執行緒池
int count= 100000; //總數
int threadCount=10000; //初始值
int beginnum= 0;
int endnum= 0;
while (count>0) {
endnum= count> threadCount? threadCount: count;
UpdateMebinfoThread thread= new UpdateMebinfoThread(beginnum, endnum);
threadPool.execute(thread);
beginnum= beginnum+ endnum;
count= count- endnum;
}
threadPool.shutdown();
}