使用FutureTask進(jìn)行多線程數(shù)據(jù)讀取泉沾,減少讀取時(shí)間
public void checkCompany(){
?? ??? ?//獲取數(shù)據(jù)
?? ??? ?List<CompanyVo> omsList = getOmsList();
?? ??? ?//將要比對(duì)數(shù)據(jù)放入Map
?? ??? ?Map<Long, CompanyVo> omsMap = new HashMap<Long, CompanyVo>();
?? ??? ?for(CompanyVo vo:omsList){
?? ??? ??? ?omsMap.put(vo.getIdUuid(), vo);
?? ??? ?}
?? ??? ?//比較數(shù)據(jù)
?? ??? ?compareCompany(omsMap);
??? }
每2000條數(shù)據(jù)比對(duì)一次贤旷,MySQL數(shù)據(jù)庫(kù)
public void compareCompany(Map<Long, CompanyVo> omsMap){
//獲取總數(shù)據(jù)量
? ? int count = jdbcTemplate.queryForObject("select count(1) from oms_company", Integer.class);
? ? //分頁(yè)查詢
? ? ? int pages = count%2000= 0?(count/2000):(count/2000 + 1);
? ? ? ExecutorService executor = Executors.newFixedThreadPool(10);
? ? ? for(int i = 0; i < pages; i++){
? ? ? int startIndex = i*2000;
? ? ? ProTask task = new ProTask(startIndex, omsMap);
? ? ? FutureTask future = new FutureTask(task,null);
? ? ? executor.execute(future);
? ? ? }
? ? ? executor.shutdown();
? ? ? //判斷是否比對(duì)完成
? ? ? while(true){
? ? ? if(executor.isTerminated()){
? ? ? break;
? ? ? }
? ? ? try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? ? }
? ? }
獲取要比較的總數(shù)據(jù),Oracle數(shù)據(jù)庫(kù)
public List<CompanyVo> getOmsList(){
? ? List<CompanyVo> list = new ArrayList<>();
? ? int count = jdbcTemplate.queryForObject("select count(1) from TB_PUBHA", Integer.class);
? ? int pages = count%2000 == 0?(count/2000):(count/2000 + 1);
? ? ExecutorService executor = Executors.newFixedThreadPool(10);
? ? List<FutureTask<List<CompanyVo>>> futureList = new ArrayList<>();
? ? for(int i = 0; i < pages; i++){
? ? int start = i*2000 + 1;
? ? int end = (i+1)*2000;
? ? if(end > count) end = count;
? ? OmsTask task = new OmsTask(start, end);
? ? FutureTask<List<CompanyVo>> future = new FutureTask<>(task);
? ? executor.submit(future);
? ? futureList.add(future);
? ? }
? ? executor.shutdown();
? ? for(FutureTask<List<CompanyVo>> future:futureList){
? ? try {
List<CompanyVo> listq = future.get();
if(null != listq && !listq.isEmpty())
list.addAll(listq);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? }
? ? return list;
? ? }
OmsTask實(shí)現(xiàn)Callable接口饶辙,因?yàn)樾枰祷刂?/p>
class OmsTask implements Callable<List<CompanyVo>>{
? ? private int start;
? ? private int end;
? ? public OmsTask(int start,int end){
? ? this.start = start;
? ? this.end = end;
? ? }
@Override
public List<CompanyVo> call() throws Exception {
String sql = "select * from (select rownum rm, PUBHA_ID id_uuid,UUID company_number,PUBHA003 company_name,PUBHA005 company_address " +
? ? ? ? ? ? ? ? ? ? ",PUBHA006 post_code,PUBHA008 corporation,PUBHA009 corporation_tel,PUBHA010 manager,PUBHA011 manager_tel,PUBHA012 contract,PUBHA013 contract_tel" +
? ? ? ? ? ? ? ? ? ? ",PUBHA015 web_url from TB_PUBHA ) where rm between "+start+" and "+end;
List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
return CompanyVo.getCompany(mapList);
}
? ? }
ProTask實(shí)現(xiàn)Runnable接口,不需要返回結(jié)果,只進(jìn)行比較
class ProTask implements Runnable{
? ? private int startIndex;
? ? private Map<Long, CompanyVo> omsMap;
? ? public ProTask(int startIndex,Map<Long, CompanyVo> omsMap){
? ? this.startIndex = startIndex;
? ? this.omsMap = omsMap;
? ? }
@Override
public void run() {
List<Map<String, Object>> listMap = jdbcTemplate.queryForList("select id_uuid,company_number,company_name,province_id,city_id,company_address,"
+ "post_code,corporation,corporation_tel,manager,manager_tel,contract,contract_tel,web_url "
+ "from oms_company limit "+startIndex+","+ 2000);
List<CompanyVo> list = CompanyVo.getCompany(listMap);
if(null != list && !list.isEmpty()){
List<Object[]> obs = new ArrayList<>();
for(CompanyVo vo:list){
CompanyVo oms = omsMap.get(vo.getIdUuid());
if(null == oms){
continue;
}
if((null == vo.getCompanyNumber() && null != oms.getCompanyNumber())
|| (null != vo.getCompanyNumber() && !vo.getCompanyNumber().equals(oms.getCompanyNumber()))){
//結(jié)果不同時(shí)進(jìn)行處理
}
//對(duì)已比對(duì)數(shù)據(jù)進(jìn)行移除
omsMap.remove(vo.getIdUuid());
}
}
}
? ? }
實(shí)體類怔檩,主要進(jìn)行查詢結(jié)果轉(zhuǎn)化
package com.digital.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.digital.util.StringUtil;
public class CompanyVo {
private Long idUuid;
private String companyNumber;
public Long getIdUuid() {
return idUuid;
}
public void setIdUuid(Long idUuid) {
this.idUuid = idUuid;
}
public String getCompanyNumber() {
return companyNumber;
}
public void setCompanyNumber(String companyNumber) {
this.companyNumber = companyNumber;
}
public static CompanyVo getCompany(Map<String, Object> map){
if(null == map) return null;
CompanyVo vo = new CompanyVo();
String idUuid = StringUtil.ObjectToString(map.get("ID_UUID"));
if(null != idUuid)
vo.setIdUuid(Long.parseLong(idUuid));
? ? ? ? vo.setCompanyNumber(StringUtil.ObjectToString(map.get("COMPANY_NUMBER")));
return vo;
}
public static List<CompanyVo> getCompany(List<Map<String, Object>> mapList){
if(null == mapList) return null;
List<CompanyVo> list = new ArrayList<>();
for(Map<String, Object> map:mapList){
CompanyVo vo = getCompany(map);
if(null != vo)
list.add(vo);
}
return list;
}
}
————————————————
版權(quán)聲明:本文為CSDN博主「yun0000000」的原創(chuàng)文章,遵循CC 4.0 by-sa版權(quán)協(xié)議蓄诽,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明薛训。
原文鏈接:https://blog.csdn.net/yun0000000/article/details/52758098