Part One: 一个线程处理
if (CollectionUtils.isNotEmpty(cqcAttachmentDataList)) {
long startTimes = System.currentTimeMillis();
final CountDownLatch latchOnlyOne = new CountDownLatch(1);
new Thread(new CqcAttachmentDataWorker(latchOnlyOne,"车前程附件数据处理-线程-only", cqcAttachmentDataList)).start();
try {
latchOnlyOne.await();
long endTimes = System.currentTimeMillis();
logger.info("所有线程(1个线程)执行完毕:" + ((endTimes - startTimes)/1000) + "秒");
} catch (InterruptedException e) {
logger.error("线程(1个线程)处理异常::::",e);
}
}
//数据处理线程worker
public class CqcAttachmentDataWorker implements Runnable{
List<TCdCqcAttachmentData> cqcAttachmentDataSubList = null;
String name = "";
CountDownLatch latch;
public CqcAttachmentDataWorker(CountDownLatch latch,String name, List<TCdCqcAttachmentData> cqcAttachmentDataListTemp){
this.name = name;
this.latch = latch;
this.cqcAttachmentDataSubList = cqcAttachmentDataListTemp;
}
@Override
public void run() {
logger.info(name + "正在执行...");
try {
//2.遍历处理
if (CollectionUtils.isNotEmpty(cqcAttachmentDataSubList)) {
//do it
}
} catch (Exception e) {
logger.error("车前程-数据处理run异常::::", e);
} finally {
latch.countDown();
}
}
}
Part Two: 多线程数据处理1-初级
List<TCdCqcAttachmentData> cqcAttachmentDataList = tCdCqcAttachmentDataService.findTCdCqcAttachmentDataAllList();
if (CollectionUtils.isNotEmpty(cqcAttachmentDataList)) {
long startTimes = System.currentTimeMillis();
int threadCount = cqcWorkerThreadCount;
int total = cqcAttachmentDataList.size();
if (total < threadCount) {
final CountDownLatch latchOnlyOne = new CountDownLatch(1);
new Thread(new CqcAttachmentDataWorker(latchOnlyOne,"车前程附件数据处理-线程-only", cqcAttachmentDataList)).start();
try {
latchOnlyOne.await();
long endTimes = System.currentTimeMillis();
logger.info("所有线程(1个线程)执行完毕:" + ((endTimes - startTimes)/1000) + "秒");
} catch (InterruptedException e) {
logger.error("线程(1个线程)处理异常::::",e);
}
} else {
int every = total / threadCount;
final CountDownLatch latch = new CountDownLatch(threadCount);
List<TCdCqcAttachmentData> cqcAttachmentDataSubList = null;
int divideRemainNumber = total % every; //取模后的余数
int divideRemainNumberTmep = total % threadCount; //取模后的余数
for (int i = 1; i <= threadCount; i++) {
int startIndex = (i - 1) * every;
int endIndex = startIndex + every;
if (total >= endIndex) {
if(divideRemainNumber > 0){
if (i == threadCount)
{
cqcAttachmentDataSubList = cqcAttachmentDataList.subList(startIndex, total);
} else {
cqcAttachmentDataSubList = cqcAttachmentDataList.subList(startIndex, startIndex + every);
}
} else {
if (divideRemainNumberTmep > 0) {
if (i == threadCount)
{
cqcAttachmentDataSubList = cqcAttachmentDataList.subList(startIndex, total);
} else {
cqcAttachmentDataSubList = cqcAttachmentDataList.subList(startIndex, startIndex + every);
}
} else {
cqcAttachmentDataSubList = cqcAttachmentDataList.subList(startIndex, startIndex + every);
}
}
}
new Thread(new CqcAttachmentDataWorker(latch,"车前程附件数据处理-线程" + i, cqcAttachmentDataSubList)).start();
}
try {
latch.await();
long endTimes = System.currentTimeMillis();
logger.info("所有线程执行完毕:" + ((endTimes - startTimes)/1000) + "秒");
} catch (InterruptedException e) {
logger.error("线程处理异常::::",e);
}
}
}
//数据处理线程worker
public class CqcAttachmentDataWorker implements Runnable{
List<TCdCqcAttachmentData> cqcAttachmentDataSubList = null;
String name = "";
CountDownLatch latch;
public CqcAttachmentDataWorker(CountDownLatch latch,String name, List<TCdCqcAttachmentData> cqcAttachmentDataListTemp){
this.name = name;
this.latch = latch;
this.cqcAttachmentDataSubList = cqcAttachmentDataListTemp;
}
@Override
public void run() {
logger.info(name + "正在执行...");
//2.遍历处理
if (CollectionUtils.isNotEmpty(cqcAttachmentDataSubList)) {
}
latch.countDown();
}
}
Part Three: 多线程数据处理2-高级-线程池实现
代码实现:
// 多线程处理
private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 1, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(50000), new ThreadPoolExecutor.CallerRunsPolicy());
if (CollectionUtils.isNotEmpty(ycAndyzTCdBusiExtendlist)) {
LinkedBlockingQueue<TCdBusiExtend> queue = new LinkedBlockingQueue<TCdBusiExtend>();
queue.addAll(ycAndyzTCdBusiExtendlist);
int size = queue.size();
CountDownLatch countDownLatch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
threadPool.execute(new CqcAttachmentDataHandler(queue, countDownLatch, cqcHandlerServiceFactory, cqcAttachmentDomain, cqcAttachmentFolder));
}
countDownLatch.await();
long poolEndTimes = System.currentTimeMillis();
logger.info("线程池对目标数据处理执行完毕:" + ((poolEndTimes - targetBeginTimes)/1000) + "秒");
long endTimes = System.currentTimeMillis();
logger.info("全部执行完毕:" + ((endTimes - startTimes)/1000) + "秒");
}
worker:
public class CqcAttachmentDataHandler implements Runnable {
private static final AppLogger logger = new AppLogger(CqcAttachmentDataHandler.class);
private LinkedBlockingQueue<TCdBusiExtend> queue;
private CountDownLatch countDownLatch;
private String cqcAttachmentDomain;
private String cqcAttachmentFolder;
private CqcHandlerServiceFactory cqcHandlerServiceFactory;
public CqcAttachmentDataHandler(LinkedBlockingQueue<TCdBusiExtend> queue, CountDownLatch countDownLatch,CqcHandlerServiceFactory cqcHandlerServiceFactoryTemp, String cqcAttachmentDomainTemp, String cqcAttachmentFolderTemp) {
this.queue = queue;
this.countDownLatch = countDownLatch;
this.cqcHandlerServiceFactory=cqcHandlerServiceFactoryTemp;
this.cqcAttachmentDomain = cqcAttachmentDomainTemp;
this.cqcAttachmentFolder = cqcAttachmentFolderTemp;
}
@Override
public void run() {
try {
TCdBusiExtend tCdBusiExtend = queue.poll();
if(tCdBusiExtend != null) {
//do it
}
} catch (Exception e) {
logger.error("CqcAttachmentDataHandler.run error", e);
} finally {
countDownLatch.countDown();
}
}
}
工厂模式:
@Service
public class CqcHandlerServiceFactory {
@Resource
private ITCdCqcAttachmentDataService tCdCqcAttachmentDataService;
@Autowired
private TCdBusiExtendMapper tCdBusiExtendMapper;
public ITCdCqcAttachmentDataService gettCdCqcAttachmentDataService() {
return tCdCqcAttachmentDataService;
}
public void settCdCqcAttachmentDataService(ITCdCqcAttachmentDataService tCdCqcAttachmentDataService) {
this.tCdCqcAttachmentDataService = tCdCqcAttachmentDataService;
}
public TCdBusiExtendMapper gettCdBusiExtendMapper() {
return tCdBusiExtendMapper;
}
public void settCdBusiExtendMapper(TCdBusiExtendMapper tCdBusiExtendMapper) {
this.tCdBusiExtendMapper = tCdBusiExtendMapper;
}
}
==========================================================
Part Four: ThreadPoolTaskExecutor的使用
<!--核心线程池--> <beanid="taskExecutor" name="taskExecutor_passive" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <propertyname="corePoolSize" value="40"/> <propertyname="maxPoolSize" value="100"/> <propertyname="queueCapacity" value="3072"/> <propertyname="rejectedExecutionHandler"> <beanclass="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /><!--调用者运行--> </property> </bean>
/** * 线程池 */ @Resource(name = "taskExecutor") private ThreadPoolTaskExecutor cartPropertyDataGetterTaskExecutor;
public ThreadPoolTaskExecutor getTaskExecutor() { return cartPropertyDataGetterTaskExecutor; } public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) { this.cartPropertyDataGetterTaskExecutor = taskExecutor; }
// 命令立减查询 if (result.isActivateKeyt() && currentCart.getKeyt() != null && StringUtil.isNotBlank(currentCart.getKeyt().getKeytStr())) { keytFuture = this.getTaskExecutor().submit(new KeytGetter(keytBS, user, siteId, currentCart.getKeyt().getKeytStr(), currentCart, commerceItemList, priceInfo, shippingGroups)); } if (blueCouponsFuture != null) { final BlueCouponSummary blueCouponSummary = buildBlueCouponSummary(blueCouponsFuture.get()); result.setBlueCouponsAmount(blueCouponSummary.getUsedAmount()); // 蓝券的使用金额 result.setBlueCouponsNum(blueCouponSummary.getUsedNum()); // 蓝券的使用张数 result.setBlueCoupons(blueCouponSummary.getList()); } if (shopCouponsFuture != null) { final ShopCouponSummary shopCouponSummary = buildShopCouponSummary(shopCouponsFuture.get()); result.setShopCouponsAmount(shopCouponSummary.getUsedAmount()); // 店铺券使用金额 result.setShopCouponsNum(shopCouponSummary.getUsedNum()); // 店铺券使用张数 result.setQueryShopCoupons(shopCouponSummary.getList()); } if (redCouponsFuture != null) { final RedCouponSummary redCouponSummary = buildRedCouponSummary(redCouponsFuture.get()); result.setRedCouponsAmount(redCouponSummary.getUsedAmount()); // 红券使用的金额 result.setRedCouponsNum(redCouponSummary.getUsedNum()); // 红券使用的张数 result.setRedCoupons(redCouponSummary.getList()); } if (prepaidCardFuture != null) { final PrepaidCardSummary prepaidCardSummary = buildPrepaidCardSummary(prepaidCardFuture.get()); result.setPrepaidCardsAmount(prepaidCardSummary.getUsedAmount()); // 预付卡使用的金额 result.setPrepaidCardsNum(prepaidCardSummary.getUsedNum()); // 预付卡使用的张数 result.setPrepaidCards(prepaidCardSummary.getList()); } VirtualAccountResult virtualAccountResult = null; if (virtualAccountResultFuture != null) { virtualAccountResult = virtualAccountResultFuture.get(); result.setVirtualAccountResult(virtualAccountResult); } if ((containsHaiWaiGou || !shouldQueryVirtualAccount(siteId, commerceItemList)) && virtualAccountResult != null) { virtualAccountResult.setPayAmount(DOUBLE_ZERO); // 可用金额为0 }
if (blueCouponsFuture != null) { final BlueCouponSummary blueCouponSummary = buildBlueCouponSummary(blueCouponsFuture.get()); result.setBlueCouponsAmount(blueCouponSummary.getUsedAmount()); // 蓝券的使用金额 result.setBlueCouponsNum(blueCouponSummary.getUsedNum()); // 蓝券的使用张数 result.setBlueCoupons(blueCouponSummary.getList()); } if (shopCouponsFuture != null) { final ShopCouponSummary shopCouponSummary = buildShopCouponSummary(shopCouponsFuture.get()); result.setShopCouponsAmount(shopCouponSummary.getUsedAmount()); // 店铺券使用金额 result.setShopCouponsNum(shopCouponSummary.getUsedNum()); // 店铺券使用张数 result.setQueryShopCoupons(shopCouponSummary.getList()); } if (redCouponsFuture != null) { final RedCouponSummary redCouponSummary = buildRedCouponSummary(redCouponsFuture.get()); result.setRedCouponsAmount(redCouponSummary.getUsedAmount()); // 红券使用的金额 result.setRedCouponsNum(redCouponSummary.getUsedNum()); // 红券使用的张数 result.setRedCoupons(redCouponSummary.getList()); } if (prepaidCardFuture != null) { final PrepaidCardSummary prepaidCardSummary = buildPrepaidCardSummary(prepaidCardFuture.get()); result.setPrepaidCardsAmount(prepaidCardSummary.getUsedAmount()); // 预付卡使用的金额 result.setPrepaidCardsNum(prepaidCardSummary.getUsedNum()); // 预付卡使用的张数 result.setPrepaidCards(prepaidCardSummary.getList()); } VirtualAccountResult virtualAccountResult = null; if (virtualAccountResultFuture != null) { virtualAccountResult = virtualAccountResultFuture.get(); result.setVirtualAccountResult(virtualAccountResult); } if ((containsHaiWaiGou || !shouldQueryVirtualAccount(siteId, commerceItemList)) && virtualAccountResult != null) { virtualAccountResult.setPayAmount(DOUBLE_ZERO); // 可用金额为0 } if (storePointFuture != null) { result.setStorePoint(storePointFuture.get()); } if (gomedoFuture != null) { result.setGomedo(gomedoFuture.get()); } if (keytFuture != null) { result.setKeyt(keytFuture.get()); }
package com.gome.pangu.trading.cart.business.taskflow.listcart.task; import java.util.List; import java.util.concurrent.Callable; import com.gome.framework.bleach.Bleacher; import com.gome.framework.logging.Logger; import com.gome.pangu.pricing.client.dto.result.PriceInfoResultRDTO; import com.gome.pangu.trading.bo.Cart; import com.gome.pangu.trading.bo.CommerceItem; import com.gome.pangu.trading.bo.ShippingGroup; import com.gome.pangu.trading.bo.User; import com.gome.pangu.trading.keyt.business.KeytBS; import com.gome.pangu.trading.keyt.client.dto.result.Keyt; /** * 命令立减查询服务Callable实现类 * Created by huangwenfeng on 2016/9/28. */ public class KeytGetter implements Callable<Keyt> { private static final Logger LOGGER = Bleacher.getLogger(KeytGetter.class); private final KeytBS keytBS; private final User user; private final String siteId; private final String keytStr; private final Cart currentCart; private final List<CommerceItem> commerceItemList; private final PriceInfoResultRDTO priceInfoResultRDTO; private final List<ShippingGroup> shippingGroups; public KeytGetter(KeytBS keytBS, User user, String siteId, String keytStr, Cart currentCart, List<CommerceItem> commerceItemList, PriceInfoResultRDTO priceInfoResultRDTO, List<ShippingGroup> shippingGroups) { this.keytBS = keytBS; this.user = user; this.siteId = siteId; this.keytStr = keytStr; this.currentCart = currentCart; this.commerceItemList = commerceItemList; this.priceInfoResultRDTO = priceInfoResultRDTO; this.shippingGroups = shippingGroups; } @Override public Keyt call() throws Exception { try { Keyt keyt = keytBS.queryKeyt(user, siteId, keytStr, currentCart, commerceItemList, priceInfoResultRDTO, shippingGroups); return keyt; } catch (Exception e) { LOGGER.error("调用命令立减查询服务失败,错误信息:", e); LOGGER.error("调用命令立减查询服务失败,用户ID:{},站点ID:{},口令串:{},Cart:{},商品列表:{},价格信息:{},配送单:{}", user.getUserId(), siteId, keytStr, currentCart, commerceItemList, priceInfoResultRDTO, shippingGroups); return null; } } }
/** * 口令立减 * Created by huangwenfeng on 2016/9/26. */ public class Keyt extends BaseModel { private static final long serialVersionUID = 4824030816608236547L; private String keytStr; // 命令串 private double remainingAmount; // 剩余支付金额(OMS备用) private double amount = 0; // 命令立减兑换到的金额 private int status = 0; // 向前端返回状态错误码 0-初始状态(没有用命令立减) 1-正常(可用命令立减) public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getKeytStr() { return keytStr; } public void setKeytStr(String keytStr) { this.keytStr = keytStr; } public double getRemainingAmount() { return remainingAmount; } public void setRemainingAmount(double remainingAmount) { this.remainingAmount = remainingAmount; } }
相关推荐
白色大气风格的旅游酒店企业网站模板.zip
python实现用户注册
Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
内容来源于网络分享,如有侵权请联系我删除。另外如果没有积分的同学需要下载,请私信我。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于计算机科学与技术等相关专业,更为适合;
内容概要:文档名为《平方表,派表集合.docx》,主要内容是1至1000的平方值以及1至1000与π的乘积结果。每个数字从1开始,逐步增加至1000,对应地计算了平方值和乘以π后的值。所有计算均通过Python脚本完成,并在文档中列出了详细的计算结果。 适合人群:需要进行数学计算或程序验证的学生、教师和研究人员。 使用场景及目标:用于快速查找特定数字的平方值或其与π的乘积,适用于教学、科研及程序测试等场景。 阅读建议:可以直接查阅所需的具体数值,无需从头到尾逐行阅读。建议在使用时配合相应的计算工具,以验证和拓展数据的应用范围。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于计算机科学与技术等相关专业,更为适合;
白色大气风格的健身私人教练模板下载.zip
白色简洁风的商务网站模板下载.zip
白色大气风格的前端设计案例展示模板.zip
内容概要:本文介绍了两个有趣的圣诞树项目方向:一是使用Arduino或Raspberry Pi开发可编程的圣诞树灯光控制系统;二是基于MATLAB开发一个圣诞树模拟器。前者通过硬件连接、编写Arduino/Raspberry Pi程序、MATLAB控制程序来实现LED灯带的闪烁;后者则通过创建圣诞树图形、添加动画效果、用户交互功能来实现虚拟的圣诞树效果。 适合人群:具备基本电子工程和编程基础的爱好者和学生。 使用场景及目标:①通过硬件和MATLAB的结合,实现实际的圣诞树灯光控制系统;②通过MATLAB模拟器,实现一个有趣的圣诞树动画展示。 阅读建议:读者可以根据自己的兴趣选择合适的项目方向,并按照步骤进行动手实践,加深对硬件编程和MATLAB编程的理解。
白色扁平风格的温室大棚公司企业网站源码下载.zip
Navicat.zip
内容概要:本文详细介绍了主成分分析(PCA)技术的原理及其在Scikit-learn库中的Python实现。首先讲解了PCA的基本概念和作用,接着通过具体示例展示了如何使用Scikit-learn进行PCA降维。内容涵盖了数据准备、模型训练、数据降维、逆转换数据等步骤,并通过可视化和实际应用案例展示了PCA的效果。最后讨论了PCA的局限性和参数调整方法。 适合人群:数据科学家、机器学习工程师、数据分析从业者及科研人员。 使用场景及目标:适用于高维数据处理,特别是在需要降维以简化数据结构、提高模型性能的场景中。具体目标包括减少计算复杂度、提高数据可视化效果和改进模型训练速度。 其他说明:本文不仅提供了详细的代码示例,还讨论了PCA在手写数字识别和机器学习模型中的应用。通过比较原始数据和降维后数据的模型性能,读者可以更好地理解PCA的影响。
VOC格式的数据集转COCO格式数据集 VOC格式的数据集转YOLO格式数据集。内容来源于网络分享,如有侵权请联系我删除。另外如果没有积分的同学需要下载,请私信我。
数字信号处理课程设计.doc
白色扁平化风格的灯饰灯具销售企业网站模板.zip
华豫佰佳组合促销视图.sql
白色大气风格的商务团队公司模板下载.zip
白色大气风格的VPS销售网站模板.zip