多執行緒解決匯出excel效能問題

2021-07-26 05:21:35 字數 2383 閱讀 2015

多執行緒解決匯出excel效能問題

第一步:controller  發起匯出資料請求

}第二步:計算總記錄數,根據總記錄數分配執行緒數和每個執行緒處理的記錄數。

@override

public string exportotherhardexcelall(string statdate, int mode, string filepath)

string fields = ;

string head = ;

int count = countotherexcelall(statdate);//計算總記錄數

int indexs = threadutils.getindex(count, 10000, 5);//根據總記錄數分配執行緒數和每個執行緒處理的記錄數

countdownlatch latch = new countdownlatch(indexs.length - 1);

for (int j = 1; j < indexs.length; j++)

string exportfilepath = getonecsv(latch, csvfilepath, head, indexs.length);

return exportfilepath;

}

附:threadutils.getindex方法,最終結果 [0,10000,20000,30000,40000,50000]

public class threadutils  else 

int each = double.valueof(math.floor(sizedb / listindexcount)).intvalue();

int indexs = new int[listindexcount + 1];

indexs[0] = 0;

int totalcount = 0;

for (int i = 1; i < listindexcount; i++)

// 最後乙個執行緒可能多分擔一點

indexs[listindexcount] = size - totalcount + indexs[listindexcount - 1];

return indexs;

}}

第三步:每個任務處理的事情  取數和匯出到excel檔案(每個執行緒匯出乙個檔案)

@override

public void run()

else

excelexport.exportcsvlocal(filepath, threadnum.tostring(), list, null, fields, i == 0);

}if (totalcount < size)

else

excelexport.exportcsvlocal(filepath, threadnum.tostring(), list, null, fields, totalcount == 0);//寫入excel檔案

}} catch (exception e) finally

}

第四步:把所有excel檔案合併到乙個檔案

private string getonecsv(countdownlatch latch, string filepath, string head, int filecount) 

file.createnewfile();

writer = new bufferedwriter(new outputstreamwriter(new fileoutputstream(filepath + "all.csv", true), "gb2312"));

for (int i = 0; i < head.length; i++)

}writer.write("\r\n");

for (int i = 1; i < filecount; i++)

reader.close();

writer.flush();

}} catch (exception e) finally

writer.close();

} catch (exception e)

}return filepath + "all.csv";

}

多執行緒匯出excel高併發 MQ解決高併發問題

最近了解到用mq解決高併發問題 本人也用過activemq,並不精通,想來想去有個問題 假設大量訪問衝進來,servlet做client把請求發給activemq,發給服務處理端,解決高併發問題 但是服務處理完,響應怎麼辦呢 莫非作為client的servlet 也訂閱乙個響應佇列麼,等服務在發回來...

多執行緒解決方案及效能

程式操作的開銷 取自 深入理解並行程式設計 表3.1 操 作 開 銷 ns 比 率 單週期指令 0.61.0 最好情況的cas 37.9 63.2 最好情況的鎖 65.6 109.3 單次快取未命中 139.5 232.5 cas快取未命中 306.0 510.0 光纖通訊 3,000 5000 全...

解決多執行緒效能問題技巧分享

效能分析工具 總結最近工作中在使用多執行緒處理業務邏輯時遇到了問題,程式執行期初,與之前未使用多執行緒沒有任何差別,但是當對應的執行緒處理佇列開始擁堵時,處理速度開始愈來愈慢,為解決該效能問題,投入多日並作出如下總結 判定原因 某方法耗時較長,拖垮整個業務流程,導致效能下降 驗證過程 在整個業務流程...