[英]Does scheduleAtFixedRate call a different thread of execution when the current execution is not complete?
[英]Thread Execution not complete in a loop
我需要使用10個線程(每個線程5000個)將50000條記錄插入數據庫。 防爆。 線程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
}
}
}
問題是,對於較大的數字,這將無法正確執行。 如果增加每個線程10000的記錄,進程將運行而不會出現任何錯誤,但只會插入批處理的一部分。 任何想法?
這是錯誤的,請閱讀評論。 :)由於線程的一致性而將其保留
您在線程中睡眠了1000毫秒。 那是1秒。
10000次插入= 10000秒= 166分鍾。
您允許線程池執行15分鍾,然后將其關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.