我需要使用10个线程将50000条记录插入数据库,每个线程5000个。 防爆。线程1将插入1-5000,线程2将插入5001-10000等。
我使用ExecutorService来执行此操作。
ExecutorService threadPool = Executors.newFixedThreadPool(10);
int i=0;
while(i<vVec.size())
{
if(i<vVec.size())
{
DBInsertDetail rrr = (DBInsertDetail)vVec.get(i);
TestThread t1 = new TestThread(rrr);
threadPool.execute(t1);
}
i++;
}
try {
threadPool.shutdown();
boolean bTermination = false;
while (true)
{
bTermination = threadPool.awaitTermination(15, TimeUnit.MINUTES);
if(!bTermination)
{
Log.debug("Awaiting completion of threads.");
}
else
{
Log.debug("Threads Completed."+iTermiVal);
break;
}
if(threadPool.isTerminated())
{
break;
}
}
} catch (Exception e) {}
public class TestThread implements Runnable
{
private volatile DBInsertDetail syncc;
public Thread1(DBInsertDetail syncc) {
this.syncc = syncc;
}
public void run() {
try
{ this.syncc.cardCreProcess(syncc.getIncre(),syncc.getStarterial(),syncc.getCurTblSeq());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class DBInsertDetail {
public void cardCreProcess(int iNum,int iCurrSerl,int iCurTblSeq)
{
int iCardCountTest = 0;
try
{
synchronized(this)
{
for (int i = 0; i < iNum; i++)
{
iCurrSerl++;
iCurTblSeq++;
iCardCountTest++;
CmnDet stkDet = new CmnDet();
Data crdData = new Data();
String sNo = crdData.getNextNo(pro, prof, sBranch, iCurrSerl);
stkDet.setNo(sNo);
stkDet.setCod(""+iCurTblSeq);
if (!stkDet.saveToDataBase(con))
{
sErrorMsg +="Error Occured" + "\n";
}
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
finally{
//commit and return connection
}
}
}
问题是,对于较大的nos,这将无法正确执行。如果增加每个线程10000的记录,则进程运行时没有任何错误,但仅插入批处理的一部分。有什么想法吗?
答案 0 :(得分:0)
这是错误的,请阅读评论。 :)由于线程的一致性而离开
你在线程中睡眠1000毫秒。那是1秒钟。
10000次插入= 10000秒= 166分钟。
允许线程池执行15分钟,然后关闭它。