java写数据多线程_Java实现多线程的三种方式

1 packagecom.tinno.adsserver.biz;2

3 importjava.util.ArrayList;4 importjava.util.Calendar;5 importjava.util.Date;6 importjava.util.HashMap;7 importjava.util.List;8 importjava.util.Map;9 importjava.util.concurrent.Callable;10 importjava.util.concurrent.ExecutionException;11 importjava.util.concurrent.ExecutorService;12 importjava.util.concurrent.Executors;13 importjava.util.concurrent.Future;14

15 importjavax.annotation.Resource;16

17 importorg.slf4j.Logger;18 importorg.slf4j.LoggerFactory;19 importorg.springframework.stereotype.Service;20

21 importcom.tinno.adsserver.base.BaseOutput;22 importcom.tinno.adsserver.constants.EnumType.HoProtocol;23 importcom.tinno.adsserver.constants.EnumType.HoRevenueType;24 importcom.tinno.adsserver.constants.EnumType.HoStatus;25 importcom.tinno.adsserver.constants.HoApiConst;26 importcom.tinno.adsserver.constants.MainConst;27 importcom.tinno.adsserver.constants.OutputCodeConst;28 importcom.tinno.adsserver.model.ho.HoApiResult;29 importcom.tinno.adsserver.model.ho.HoThreadBack;30 importcom.tinno.adsserver.service.HoSyncService;31 importcom.tinno.adsserver.utils.DateUtil;32 importcom.tinno.adsserver.utils.HoApiUtil;33 importcom.tinno.network.core.paging.QueryFilter;34

35 /**

36 * HasOffer操作Biz
37 *38 *@authorlu.wei
39 * @email 1025742048@qq.com
40 * @date 2016年12月29日
41 *@version1.0
42 *@sinceV1.0
43 *@seecom.tinno.adsserver.biz
44 */

45 @Service("hasOfferOperateBiz")46 public classHasOfferOperateBiz {47 private Logger logger =LoggerFactory.getLogger(getClass());48

49 @Resource(name = "hoSyncService")50 privateHoSyncService hoSyncService;51

52 public BaseOutput syncOfferToHasOffer(QueryFilter filter) throwsInterruptedException, ExecutionException {53 BaseOutput out = newBaseOutput();54

55 Date now = newDate();56 long tm =now.getTime();57 int sum = 0;58 int addCnt = 0;59 int disableCnt = 0;60 int updateCnt = 0;61 int addFailCnt = 0;62 int disableFailCnt = 0;63 int updateFailCnt = 0;64 List addFailIds = new ArrayList();65 List disableFailIds = new ArrayList();66 List updateFailIds = new ArrayList();67

68 filter.getParam().put(MainConst.KEY_TM, String.valueOf(tm));69 //为了防止长事务所表,将业务层的操作拆分出来70 //1、进行预占数据

71 int occupyCnt =hoSyncService.updateNotSyncOfferToOccupy(filter);72

73 //2、获取占用数据

74 List> changedOffers = new ArrayList>();75 if (occupyCnt > 0) {76 changedOffers =hoSyncService.selectFullOccupyChangeOffer(filter);77 }78

79 //3、生成失效时间

80 Calendar calendar =Calendar.getInstance();81 calendar.setTime(now);82 calendar.add(Calendar.YEAR, 1);83 Date yearDate =calendar.getTime();84 String expirDate =DateUtil.transferDateToStr(yearDate, DateUtil.DF_PATTERN_YYYY_MM_DD_HHMMSS);85 //4、开始处理数据

86 if (null != changedOffers && !changedOffers.isEmpty()) {87 sum =changedOffers.size();88 //计算需要启动的线程数量89 //每300条记录启动一个线程

90 int threadCnt = (int) Math.ceil(sum / 300.0);91 ExecutorService pool =Executors.newFixedThreadPool(threadCnt);92 List> futures = new ArrayList>();93 for (int i = 0; i < threadCnt; i++) {94 int startIndex = i * 300;95 int endIndex = (i + 1) * 300;96 if (endIndex >sum) {97 endIndex =sum;98 }99 List> subOfferData =changedOffers.subList(startIndex, endIndex);100

101 Future future =getDealSyncOfferFuture(pool, subOfferData, filter, expirDate);102 futures.add(future);103 }104

105 int executeCnt = 0;106 List backs = new ArrayList();107 while (executeCnt > forEachFutures = new ArrayList>();109 for (Futurefuture : futures) {110 if(future.isDone()) {111 backs.add(future.get());112 forEachFutures.add(future);113 executeCnt++;114 }115 }116 futures.removeAll(forEachFutures);117 }118 pool.shutdown();119 if (!backs.isEmpty()) {120 for(HoThreadBack back : backs) {121 if(OutputCodeConst.SUCCESS.equals(back.getCode())) {122 addCnt +=back.getAddCnt();123 updateCnt +=back.getUpdateCnt();124 disableCnt +=back.getDisableCnt();125 addFailCnt +=back.getAddFailCnt();126 updateFailCnt +=back.getUpdateFailCnt();127 disableFailCnt +=back.getDisableFailCnt();128

129 addFailIds.addAll(back.getAddFailIds());130 updateFailIds.addAll(back.getUpdateFailIds());131 disableFailIds.addAll(back.getDisableFailIds());132 }133 else{134 logger.error(back.getMsg());135 }136 }137 }138 }139

140 //释放占用数据

141 int unOccupyCnt =hoSyncService.updateOccupyedOfferToReleased(filter);142

143 StringBuffer sb = new StringBuffer("");144 sb.append(" 执行同步Offer数据如下: 需要同步的数据总量为:").append(occupyCnt);145 sb.append("; 能够同步的数据量为:").append(sum);146 sb.append("; 新增成功的数据量为:").append(addCnt);147 sb.append("; 更新成功的数据量为:").append(updateCnt);148 sb.append("; 禁用成功的数据量为:").append(disableCnt);149 sb.append("; 新增失败的数据量为:").append(addFailCnt);150 sb.append("; 更新失败的数据量为:").append(updateFailCnt);151 sb.append("; 禁用失败的数据量为:").append(disableFailCnt);152 sb.append("; 释放的数据量为:").append(unOccupyCnt);153 sb.append("; 新增失败的Offer为:").append(addFailIds);154 sb.append("; 更新失败的Offer为:").append(updateFailIds);155 sb.append("; 禁用失败的Offer为:").append(disableFailIds);156 out.setMsg(out.getMsg() +sb.toString());157

158 returnout;159 }160

161 /**

162 *163 * 获取执行Future
164 *165 *@authorlu.wei
166 * @email 1025742048@qq.com
167 * @date 2016年12月29日 上午9:55:50168 *@paramthreadPool169 *@paramofferDatas170 *@paramfilter171 *@paramexpirDate172 *@return
173 */

174 private Future getDealSyncOfferFuture(ExecutorService threadPool, final List>offerDatas,175 final QueryFilter filter, finalString expirDate) {176 return threadPool.submit(new Callable() {177 @Override178 public HoThreadBack call() throwsException {179 returnupdateOfferToHO(offerDatas, filter, expirDate);180 }181 });182 }183

184 /**

185 *186 * 同步Offer到HasOffers
187 *188 *@authorlu.wei
189 * @email 1025742048@qq.com
190 * @date 2016年12月29日 下午3:20:39191 *@paramofferDatas192 *@paramfilter193 *@paramexpirDate194 *@return
195 */

196 private HoThreadBack updateOfferToHO(List>offerDatas, QueryFilter filter, String expirDate) {197 String threadName =Thread.currentThread().getName();198 HoThreadBack threadBack = newHoThreadBack();199 int sum =offerDatas.size();200 int addCnt = 0;201 int disableCnt = 0;202 int updateCnt = 0;203 int addFailCnt = 0;204 int disableFailCnt = 0;205 int updateFailCnt = 0;206 List> newHasOfferIds = new ArrayList>();207 List needUpdateIds = new ArrayList();208

209 logger.info("-----------------------线程{}执行-------------------------------------Begin", threadName);210

211 BaseOutput out = newBaseOutput();212 String hasOfferId = null;213 int i = 1;214 for (MapofferData : offerDatas) {215 logger.info("线程 {} 总共需要执行的数据 {} 当前正在执行的是第{}个", threadName, sum, i);216 String id = String.valueOf(offerData.get("id"));217 filter.getParam().put("offerId", id);218 Object hasofferIdObj = offerData.get("hasofferId");219 //执行新增操作

220 if (null == hasofferIdObj || "".equals(String.valueOf(hasofferIdObj))) {221 long begin =System.currentTimeMillis();222 //稍后批量执行新增操作223 //hasOfferId = hoSyncService.addRemoteHoOffer(offerData,224 //expirDate);

225 hasOfferId =addRemoateHoOffer(offerData, expirDate);226 logger.info("线程 {} 执行新增Offer({})到 HasOffer耗时{}", threadName, id, (System.currentTimeMillis() -begin));227 if (null !=hasOfferId) {228 addCnt += 1;229 Map idMap = new HashMap();230 idMap.put(MainConst.KEY_ID, id);231 idMap.put(MainConst.KEY_CA_HASOFFERSOFFERID, hasOfferId);232 newHasOfferIds.add(idMap);233 }234 else{235 addFailCnt++;236 threadBack.getAddFailIds().add(id);237 }238 }239 //执行更新操作

240 else{241 hasOfferId =String.valueOf(hasofferIdObj);242 String status = String.valueOf(offerData.get("status"));243 //修改Offer为禁用状态

244 if ("0".equals(status)) {245 out =updateRemoteHoOfferToDisabled(offerData);246

247 if(out.getCode().equals(OutputCodeConst.SUCCESS)) {248 disableCnt += 1;249 needUpdateIds.add(id);250 }251 else{252 disableFailCnt++;253 threadBack.getDisableFailIds().add(id);254 }255 }256 //执行更新目前只更新Offer Url

257 else{258 out =updateRemoteHoOffer(offerData);259 if(out.getCode().equals(OutputCodeConst.SUCCESS)) {260 updateCnt += 1;261 needUpdateIds.add(id);262 }263 else{264 updateFailCnt++;265 threadBack.getUpdateFailIds().add(id);266 }267 }268 }269 i++;270 }271

272 //存在新增Offer

273 if (!newHasOfferIds.isEmpty()) {274 filter.getExtraParam().put(MainConst.KEY_DATAS, newHasOfferIds);275 hoSyncService.updateUnOfferdOfferToNotChange(filter);276 }277

278 //存在需要更新的Offer

279 if (!needUpdateIds.isEmpty()) {280 filter.getExtraParam().put(MainConst.KEY_NEED_UPDATE_IDS, needUpdateIds);281 hoSyncService.updateOfferdOfferToNotChange(filter);282 }283

284 threadBack.setCode(out.getCode());285 threadBack.setMsg(out.getMsg());286 threadBack.setAddCnt(addCnt);287 threadBack.setUpdateCnt(updateCnt);288 threadBack.setDisableCnt(disableCnt);289 threadBack.setAddFailCnt(addFailCnt);290 threadBack.setDisableFailCnt(disableFailCnt);291 threadBack.setUpdateFailCnt(updateFailCnt);292

293 logger.info(threadBack.toString());294 logger.info("-----------------------线程{}执行-------------------------------------End", threadName);295 returnthreadBack;296 }297

298 /**

299 *300 * 将Offer新增到HasOffer
301 *302 *@authorlu.wei
303 * @email 1025742048@qq.com
304 * @date 2016年12月30日 下午2:09:52305 *@paramofferData306 *@paramexpirDate307 *@return
308 */

309 private String addRemoateHoOffer(MapofferData, String expirDate) {310 Object idObj = offerData.get("id");311 logger.info("---------------addRemoteHoOffer({})-----------------------Begin", idObj);312

313 String hasOfferId = null;314 try{315 Map params = new HashMap();316 params.put(HoApiConst.KEY_ADVERTISER_ID, String.valueOf(offerData.get("advertiserId")));317 params.put(MainConst.KEY_NAME, String.valueOf(offerData.get(MainConst.KEY_NAME)));318 if (null !=offerData.get(MainConst.KEY_DESCRIPTION)) {319 params.put(MainConst.KEY_DESCRIPTION, String.valueOf(offerData.get(MainConst.KEY_DESCRIPTION)));320 }321 params.put(HoApiConst.KEY_PREVIEW_URL, String.valueOf(offerData.get("clickUrl")));322 params.put(HoApiConst.KEY_OFFER_URL, String.valueOf(offerData.get("clickUrl")));323 params.put(HoApiConst.KEY_STATUS, HoStatus.Active.getValue());324 params.put(HoApiConst.KEY_EXPIRATION_DATE, expirDate);325 params.put(MainConst.KEY_CURRENCY, String.valueOf(offerData.get(MainConst.KEY_CURRENCY)));326 params.put(MainConst.KEY_PROTOCOL, HoProtocol.HttpiFramePixel.getValue());327 logger.info(params.toString());328 if (null != offerData.get("revenueType")) {329 String localRevenueType = String.valueOf(offerData.get("revenueType"));330 String revenueType =HoRevenueType.CPC.getValue();331 if ("cpi".equals(localRevenueType)) {332 revenueType =HoRevenueType.CPA_FLAT.getValue();333 }334 params.put(HoApiConst.KEY_REVENUE_TYPE, revenueType);335 }336 if (null != offerData.get("revenueRate")) {337 params.put(HoApiConst.KEY_MAX_PAYOUT, String.valueOf(offerData.get("revenueRate")));338 }339

340 HoApiResult addResult =HoApiUtil.getHoOperateResult(HoApiConst.OFFER_CREATE_OFFER, params);341 String status =addResult.getResponse().getStatus();342 if (null != status &&HoApiConst.KEY_STATUS_SUCCESS.equals(status)) {343 logger.info("HasOffers执行新增Offer成功!");344 Map data = addResult.getResponse().getDatas().get(0);345 hasOfferId =data.get(MainConst.KEY_ID);346 }347 else{348 logger.error("执行HasOffer接口失败: {} ", addResult.getResponse().getErrorMessage());349 }350 }351 catch(Exception e) {352 logger.error("执行addRemoteHoOffer({})接口失败: {} ", idObj, e.getMessage(), e);353 }354 logger.info("---------------addRemoteHoOffer({})-----------------------End", idObj);355 returnhasOfferId;356 }357

358 /**

359 *360 * 更新远程HasOffer信息
361 *362 *@authorlu.wei
363 * @email 1025742048@qq.com
364 * @date 2016年12月30日 下午2:24:14365 *@paramofferData366 *@return
367 */

368 private BaseOutput updateRemoteHoOffer(MapofferData) {369 Object idObj = offerData.get("id");370 logger.info("---------------updateRemoteHoOffer({})-----------------------Begin", idObj);371 BaseOutput out = newBaseOutput();372 try{373 String hasOfferId = String.valueOf(offerData.get("hasofferId"));374 Map dataParams = new HashMap();375 dataParams.put(HoApiConst.KEY_ADVERTISER_ID, String.valueOf(offerData.get("advertiserId")));376 dataParams.put(MainConst.KEY_NAME, String.valueOf(offerData.get(MainConst.KEY_NAME)));377 if (null !=offerData.get(MainConst.KEY_DESCRIPTION)) {378 dataParams.put(MainConst.KEY_DESCRIPTION, String.valueOf(offerData.get(MainConst.KEY_DESCRIPTION)));379 }380 dataParams.put(HoApiConst.KEY_PREVIEW_URL, String.valueOf(offerData.get("clickUrl")));381 dataParams.put(HoApiConst.KEY_OFFER_URL, String.valueOf(offerData.get("clickUrl")));382 dataParams.put(HoApiConst.KEY_STATUS, HoStatus.Active.getValue());383 dataParams.put(MainConst.KEY_CURRENCY, String.valueOf(offerData.get(MainConst.KEY_CURRENCY)));384 dataParams.put(MainConst.KEY_PROTOCOL, HoProtocol.HttpiFramePixel.getValue());385

386 if (null != offerData.get("revenueType")) {387 String localRevenueType = String.valueOf(offerData.get("revenueType"));388 String revenueType =HoRevenueType.CPC.getValue();389 if ("cpi".equals(localRevenueType)) {390 revenueType =HoRevenueType.CPA_FLAT.getValue();391 }392 dataParams.put(HoApiConst.KEY_REVENUE_TYPE, revenueType);393 }394 if (null != offerData.get("revenueRate")) {395 dataParams.put(HoApiConst.KEY_DEFAULT_PAYOUT, String.valueOf(offerData.get("revenueRate")));396 }397

398 Map urlParams = new HashMap();399 urlParams.put(MainConst.KEY_ID, hasOfferId);400

401 HoApiResult addResult =HoApiUtil.getHoOperateResult(HoApiConst.OFFER_UPDATE_OFFER, urlParams, dataParams);402 String status =addResult.getResponse().getStatus();403 if (null != status &&HoApiConst.KEY_STATUS_SUCCESS.equals(status)) {404 logger.info("HasOffers执行修改Offer成功!");405 }406 else{407 out.setCode(OutputCodeConst.UNKNOWN_ERROR);408 out.setMsg("HasOffers执行修改Offer失败!");409 logger.error("执行updateRemoteHoOffer({})接口失败: {} ", idObj, addResult.getResponse().getErrorMessage());410 }411 }412 catch(Exception e) {413 out.setCode("-999");414 out.setMsg("HasOffers执行修改Offer状态失败!");415 logger.error("执行updateRemoteHoOffer({})接口失败: {} ", idObj, e.getMessage(), e);416 }417 logger.info("---------------updateRemoteHoOffer({})-----------------------End", idObj);418 returnout;419 }420

421 /**

422 *423 * 禁用远程HasOffer
424 *425 *@authorlu.wei
426 * @email 1025742048@qq.com
427 * @date 2016年12月30日 下午2:26:54428 *@paramofferData429 *@return
430 */

431 private BaseOutput updateRemoteHoOfferToDisabled(MapofferData) {432 Object idObj = offerData.get("id");433 logger.info("---------------updateRemoteHoOfferToDisabled({})-----------------------Begin", idObj);434

435 BaseOutput out = newBaseOutput();436 try{437 String hasOfferId = String.valueOf(offerData.get("hasofferId"));438

439 Map dataParams = new HashMap();440 dataParams.put(HoApiConst.KEY_STATUS, HoStatus.Expired.getValue());441

442 Map urlParams = new HashMap();443 urlParams.put(MainConst.KEY_ID, hasOfferId);444

445 HoApiResult addResult =HoApiUtil.getHoOperateResult(HoApiConst.OFFER_UPDATE_OFFER, urlParams, dataParams);446 String status =addResult.getResponse().getStatus();447 if (null != status &&HoApiConst.KEY_STATUS_SUCCESS.equals(status)) {448 logger.info("HasOffers执行修改Offer状态成功!");449 }450 else{451 out.setCode("-999");452 out.setMsg("HasOffers执行修改Offer状态失败!");453 logger.error("执行updateRemoteHoOfferToDisabled({})接口失败: {} ", idObj, addResult.getResponse().getErrorMessage());454 }455 }456 catch(Exception e) {457 out.setCode("-999");458 out.setMsg("HasOffers执行修改Offer状态失败!");459 logger.error("执行updateRemoteHoOfferToDisabled({})接口失败: {} ", idObj, e.getMessage(), e);460 }461 logger.info("---------------updateRemoteHoOfferToDisabled({})-----------------------End", idObj);462 returnout;463 }464 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值