多執行緒優化

2021-09-02 13:29:29 字數 2924 閱讀 1654

public void newexecute() 

executorservice service = executors.newfixedthreadpool(threadnum);

//所有賬號總數

int accountcount = accountdao.findallmerchantaccountcount();

int latchcount = accountcount % batch_size == 0 ? accountcount / batch_size : (accountcount / batch_size + 1);

final countdownlatch latch = new countdownlatch(latchcount);

final date bizdate = dateutils.adddate(dateutils.removetime(new date()), -1);

final date lastbizdate = dateutils.adddate(bizdate, -1);

int offset = 0;

while (offset < accountcount)

} catch (exception e) finally }};

service.submit(run);

}try catch (interruptedexception e)

}

上面的**沒起到優化作用,下面的**優化比較好

public string execute() throws exception 

string strthreadnum= lionconfigutils.getproperty("ts-tg-settle-service.tgsettlegroupjob.groupthreadnum");

if(isnumeric(strthreadnum))

executorservice service = executors.newfixedthreadpool(threadnum);

//得到所有要發的數量和latch數量。

int resultcount=tstgsettlegroupdao.selectcountsettlegrouptotuangoujob(starttime,endtime,statuslist);

int latchcount=0;

if(resultcount%pagesize==0)else

countdownlatch latch=new countdownlatch(latchcount);

//查詢每頁資料並傳送 直到查沒有了

int lastid = 0;

listentitylist = null;

while(true)else

if(collectionutils.isempty(entitylist))

" send group : pagesize: "+pagesize +" startrow:"+startrow +" starttime: "+starttime+" endtime: "+endtime);

" send group : pagesize: "+pagesize +" startrow:"+startrow +" starttime: "+starttime+" endtime: "+endtime);

service.submit(new managelistandsendthread(tstgsettlegroupdao,entitylist,settleaccountmessageproducer,latch,sleeptime));

lastid = entitylist.get(entitylist.size() - 1).getid();

} try catch (exception e)

return null;

}

public class managelistandsendthread implements runnablecatch (exception e)

}}finally

}}

這段**結合多執行緒對大資料量下的分頁查詢進行了優化,sql語句參考下面

select id,accountid,accounttype,accountsubject,credit,debit,status,isbuffer,accountdirection,balance,frozenbalance,addtime,updatetime

from account

where accounttype = 2

order by id

limit 1825000, 10;

select id,accountid,accounttype,accountsubject,credit,debit,status,isbuffer,accountdirection,balance,frozenbalance,addtime,updatetime

from account

where accounttype = 2 and id >= 2790048

order by id asc

limit 10;

select id,accountid,accounttype,accountsubject,credit,debit,status,isbuffer,accountdirection,balance,frozenbalance,addtime,updatetime

from account

where accounttype = 2 and id >(

select id from account order by id limit 1825000,1

) order by id limit 10;

webpack優化 速度優化 多執行緒

1 背景 對於webpack來說,預設的配置都是單執行緒的,並沒有充分利用電腦cpu的資源,可以充分利用cpu實現多執行緒打包和壓縮專案,以達到節省編譯時間的目的。2 介紹 使用 1 webpack parallel uglify plugin webpack預設提供的uglifyjs外掛程式,由於...

多執行緒 Synchronized的優化

對synchronized不太了解的同學,可以先參考我的另外一篇文章 多執行緒 淺說synchronized 在早期版本中,synchronized是一種重量級鎖,其底層由monitor實現,而monitor又依賴於作業系統的mutex lock。執行緒獲取到鎖後,需要切換狀態,而作業系統在實現執行...

pyqt5多執行緒優化 PyQt5多執行緒

您不應該向主檢視傳送乙個新的小部件 qlabel 因為它將是乙個新標籤,而不是原始標籤,您應該傳送str型別的文字class thread qthread changepixmap pyqtsignal qpixmap changelabel pyqtsignal str def run self ...