当前位置: 首页 > news >正文

网站怎么做啊网络代运营推广

网站怎么做啊,网络代运营推广,网站建设设计案例网站logo实验报告,北京注册公司代理机构排名目录 前言 CountDownLatch是什么#xff1f; PriorityBlockingQueue是什么#xff1f; 场景描述 解决方案 定义统一工厂制造类 定义制造厂 定义客户请求实现 定义控制器 定义启动类 结果呈现 启动项目 请求制造操作 总结 前言 写这篇文章的缘由是因为之前在面…目录 前言 CountDownLatch是什么 PriorityBlockingQueue是什么 场景描述 解决方案 定义统一工厂制造类 定义制造厂 定义客户请求实现 定义控制器 定义启动类 结果呈现 启动项目 请求制造操作 总结 前言 写这篇文章的缘由是因为之前在面试期间经常被提到的一个场景题“前端向后端发起一个API请求该API需要处理复杂的业务逻辑涉及多个相互独立的业务模块。每个业务模块都需要执行特定的操作且这些操作彼此之间没有依赖关系。然而每个模块的处理都需要一定的时间导致整体的接口响应时间较长请给出优化接口的方案而且结果必须通过当前接口返回”。或许大家立马想到的都是通过多线程或者通过队列异步来完成结果延迟返回问题的难点在于怎么能在当前接口返回最终的结果呢在学习完RocketMq源码后我找到了最佳方案。多线程Runnable结合CountDownLatch以及PriorityBlockingQueue就是答案。 CountDownLatch是什么 CountDownLatch 是 Java 并发工具库中的一个类用于同步一个或多个线程确保某些操作在其他操作完成之前不会继续执行。它能够使一个线程等待其他线程完成各自的工作后再继续执行。 读完CountDownLatch的概述我们大概能猜出它在方案中的作用了吧描述得也很清晰了。就是在场景是主线程启动了多个工作线程并等待所有工作线程完成工作后再继续。 主要方法 void await()使当前线程等待直到计数到达零释放。boolean await(long timeout, TimeUnit unit)使当前线程等待直到计数到达零或者等待超时才释放。void countDown()递减计数如果计数到达零则释放所有等待的线程。long getCount()返回当前计数。 PriorityBlockingQueue是什么 PriorityBlockingQueue 是 Java 并发包 (java.util.concurrent) 提供的一个线程安全的无界优先级队列。它结合了优先级队列和阻塞队列的特点在多线程环境下非常有用。默认使用元素的自然顺序通过实现 Comparable 接口的 compareTo 方法。你也可以通过提供自定义的 Comparator 实现定制排序逻辑。 在本次方案中就是作为一个内存队列通知其他独立的业务模块执行操作。 主要方法 boolean add(E e) / boolean offer(E e) 插入指定元素返回 true 表示插入成功。E take() 检索并移除队列的头部元素如果队列为空则等待直到有元素可用。E poll(long timeout, TimeUnit unit) 检索并移除队列的头部元素如果队列为空则等待指定的时间。E peek() 检索但不移除队列的头部元素如果队列为空则返回 null。int size() 返回队列中的元素数量。 场景描述 根据上面问题我们模拟一个场景一个客户订购了一架飞机、一艘轮船、一辆汽车但是飞机、轮船、汽车各自的工厂都相隔很远客户想以最短的时间同时拥有它们但生性多疑的他为了防止制造厂商偷工减料他提出想亲自监工这三个交通工具的制造。请问客户与制造厂该怎么配合才能在客户亲自监工的情况下又能最快时间的同时拥有汽车、轮船和飞机呢 一架飞机的制造时间为4s一艘轮船的制造时间为3s一辆汽车的制造时间为2s 方案一传统方案就是客户先去飞机厂通知开始制造飞机并现场监督飞机制造完成后再去轮船厂其次再去汽车厂顺序任意抛出中间路程的时间总共用时至少得 4329s,时间太长客户不接受。 方案二客户通过手机同时给飞机、轮船以及汽车厂发生消息通知他们开始制造并且在三个厂商制造间安装监控客户通过同时监督它们制造因为飞机制造时间最长那么当飞机制造完成轮船和汽车肯定也已经完成了那么总共用时最快4s完美解决。 解决方案 GitHub源码地址点击获取 首先通过idea创建一个springboot项目 定义统一工厂制造类 每个工厂都有制造方法FactoryService public interface FactoryService {//制造boolean factoryOfManufacture(); } 定义制造厂 飞机、轮船、汽车都是独立的制造厂家所以需要三个独立的线程来处理. 定义公共抽象线程类ServiceThread并实现多线程Runnablel类的启动方法start() Slf4j public abstract class ServiceThread implements Runnable{protected Thread thread;protected boolean isDaemon false;//Make it able to restart the threadprivate final AtomicBoolean started new AtomicBoolean(false);public ServiceThread() {}//获取线程名称public abstract String getServiceName();//线程启动方法public void start() {log.info(Try to start service thread:{} started:{} lastThread:{}, getServiceName(), started.get(), thread);if (!started.compareAndSet(false, true)) {return;}this.thread new Thread(this, getServiceName());this.thread.setDaemon(isDaemon);this.thread.start();log.info(Start service thread:{} started:{} lastThread:{}, getServiceName(), started.get(), thread);} } 定义飞机厂类AirPlaneService基础线程类ServiceThread以及实现工厂类FactoryService Service Slf4j public class AirPlaneService extends ServiceThread implements FactoryService {public static ConcurrentMapString, AirplaneRequest requestTable new ConcurrentHashMap();//接收制造请求通知public static PriorityBlockingQueueAirplaneRequest requestQueue new PriorityBlockingQueue();//请求通知静态内部类对象public static class AirplaneRequest implements ComparableAirplaneRequest {//线程完成设置为0private CountDownLatch countDownLatch new CountDownLatch(1);//用户idprivate String userId;public CountDownLatch getCountDownLatch() {return countDownLatch;}public void setCountDownLatch(CountDownLatch countDownLatch) {this.countDownLatch countDownLatch;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId userId;}Overridepublic int compareTo(AirplaneRequest o) {return 0;}}//获取当前线程名称并设置线程名称Overridepublic String getServiceName() {return AirPlaneService.class.getSimpleName();}//执行线程Overridepublic void run() {log.info(飞机工厂启动-----------);//循环处理不同的请求通知while (this.factoryOfManufacture()) ;}public boolean factoryOfManufacture() {boolean isSuccess false;AirplaneRequest airplaneRequest null;try {//等待飞机制造请求airplaneRequest requestQueue.take();log.info(开始飞机制造-----------);//校验数据是否合法AirplaneRequest expectedRequest this.requestTable.get(airplaneRequest.getUserId());if (null expectedRequest) {log.warn(this mmap request expired, maybe cause timeout airplaneRequest.getUserId());return true;}if (expectedRequest ! airplaneRequest) {log.warn(never expected here, maybe cause timeout airplaneRequest.getUserId());return true;}//...业务处理Thread.sleep(4000);//...isSuccess true;} catch (InterruptedException e) {log.warn(this.getServiceName() interrupted, possibly by shutdown.);return false;} finally {if (airplaneRequest ! null isSuccess) {log.info(飞机制造完成啦-----------);airplaneRequest.getCountDownLatch().countDown();}}return true;} } 定义轮船厂类ShipService基础线程类ServiceThread以及实现工厂类FactoryService Service Slf4j public class ShipService extends ServiceThread implements FactoryService {public static ConcurrentMapString, ShipRequest requestTable new ConcurrentHashMap();public static PriorityBlockingQueueShipRequest requestQueue new PriorityBlockingQueue();public static class ShipRequest implements ComparableShipRequest {private CountDownLatch countDownLatch new CountDownLatch(1);private String userId;public CountDownLatch getCountDownLatch() {return countDownLatch;}public void setCountDownLatch(CountDownLatch countDownLatch) {this.countDownLatch countDownLatch;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId userId;}Overridepublic int compareTo(ShipRequest o) {return 0;}}Overridepublic String getServiceName() {return ShipService.class.getSimpleName();}Overridepublic void run() {log.info(轮船工厂启动-----------);while (this.factoryOfManufacture()) ;}Overridepublic boolean factoryOfManufacture() {boolean isSuccess false;ShipRequest shipRequest null;try {shipRequest requestQueue.take();log.info(开始制造轮船-----------);//校验数据是否合法ShipRequest expectedRequest this.requestTable.get(shipRequest.getUserId());if (null expectedRequest) {log.warn(this mmap request expired, maybe cause timeout shipRequest.getUserId());return true;}if (expectedRequest ! shipRequest) {log.warn(never expected here, maybe cause timeout shipRequest.getUserId());return true;}//...业务处理Thread.sleep(3000);//...isSuccess true;} catch (InterruptedException e) {log.warn(this.getServiceName() interrupted, possibly by shutdown.);return false;} finally {if (shipRequest ! null isSuccess) {log.info(轮船制造完成啦-----------);shipRequest.getCountDownLatch().countDown();}}return true;} } 定义汽车厂类CarService基础线程类ServiceThread以及实现工厂类FactoryService Service Slf4j public class CarService extends ServiceThread implements FactoryService {public static ConcurrentMapString, CarRequest requestTable new ConcurrentHashMap();public static PriorityBlockingQueueCarRequest requestQueue new PriorityBlockingQueue();public static class CarRequest implements ComparableCarRequest {private CountDownLatch countDownLatch new CountDownLatch(1);private String userId;public CountDownLatch getCountDownLatch() {return countDownLatch;}public void setCountDownLatch(CountDownLatch countDownLatch) {this.countDownLatch countDownLatch;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId userId;}Overridepublic int compareTo(CarRequest o) {return 0;}}Overridepublic String getServiceName() {return CarService.class.getSimpleName();}Overridepublic void run() {log.info(汽车工厂启动-----------);while (this.factoryOfManufacture());}Overridepublic boolean factoryOfManufacture() {boolean isSuccess false;CarRequest carRequest null;try {carRequest requestQueue.take();log.info(开始汽车制造-----------);//校验数据是否合法CarRequest expectedRequest this.requestTable.get(carRequest.getUserId());if (null expectedRequest) {log.warn(this mmap request expired, maybe cause timeout carRequest.getUserId());return true;}if (expectedRequest ! carRequest) {log.warn(never expected here, maybe cause timeout carRequest.getUserId());return true;}//...业务处理Thread.sleep(2000);//...isSuccess true;} catch (InterruptedException e) {log.warn(this.getServiceName() interrupted, possibly by shutdown.);return false;} finally {if (carRequest ! null isSuccess) {log.info(汽车制造完成啦-----------);carRequest.getCountDownLatch().countDown();}}return true;} } 定义客户请求实现 Service public interface PurchaseService {//请求制造Boolean manufacturing(String idCard); } 实现逻辑类 Service Slf4j public class PurchaseServiceImpl implements PurchaseService {//超时时间private static int waitTimeOut 1000 * 5;Autowiredprivate AirPlaneService airPlaneService;Autowiredprivate CarService carService;Autowiredprivate ShipService shipService;Overridepublic Boolean manufacturing(String userId) {long startTime System.currentTimeMillis();//通知飞机厂计算飞机制造AirPlaneService.AirplaneRequest airplaneRequest new AirPlaneService.AirplaneRequest();airplaneRequest.setUserId(userId);airPlaneService.requestTable.put(userId, airplaneRequest);airPlaneService.requestQueue.offer(airplaneRequest);//通知汽车厂计算汽车制造CarService.CarRequest carRequest new CarService.CarRequest();carRequest.setUserId(userId);carService.requestTable.put(userId, carRequest);carService.requestQueue.offer(carRequest);//通知轮船厂计算轮船制造ShipService.ShipRequest shipRequest new ShipService.ShipRequest();shipRequest.setUserId(userId);shipService.requestTable.put(userId, shipRequest);shipService.requestQueue.offer(shipRequest);//获取飞机制造AirPlaneService.AirplaneRequest airplaneOK airPlaneService.requestTable.get(userId);//获取飞机制造CarService.CarRequest carOK carService.requestTable.get(userId);//获取轮船制造ShipService.ShipRequest shipOK shipService.requestTable.get(userId);try {//等待获取制造结果if (airplaneOK ! null carOK! null shipOK! null) {boolean waitOK airplaneOK.getCountDownLatch().await(waitTimeOut, TimeUnit.MILLISECONDS);boolean waitOK2 carOK.getCountDownLatch().await(waitTimeOut, TimeUnit.MILLISECONDS);boolean waitOK3 shipOK.getCountDownLatch().await(waitTimeOut, TimeUnit.MILLISECONDS);//如果都成功了返回trueif (waitOK waitOK2 waitOK3) {log.info(总共用时(System.currentTimeMillis() - startTime));airPlaneService.requestTable.remove(userId);carService.requestTable.remove(userId);shipService.requestTable.remove(userId);return true;}}} catch (InterruptedException e) {Thread.currentThread().interrupt();throw new RuntimeException(e);}System.out.println(失败了总共用时(System.currentTimeMillis() - startTime));return false;} } 定义控制器 RestController public class UserController {Autowiredprivate PurchaseService purchaseService;GetMapping(/manufacturing)public Boolean manufacturing(String userId) {return purchaseService.manufacturing(userId);} } 定义启动类 同时启动飞机、轮船以及汽车的厂家线程 SpringBootApplication public class AsyncOneApplication {public static void main(String[] args) {SpringApplication.run(AsyncOneApplication.class, args);//启动飞机制造线程AirPlaneService airPlaneService new AirPlaneService();airPlaneService.start();//启动汽车制造线程CarService carService new CarService();carService.start();//启动轮船制造线程ShipService shipService new ShipService();shipService.start();} } 结果呈现 启动项目 可以看到飞机、汽车以及轮船各自的线程都启动了并成功设置了对应的线程名称 并且第一条日志输出成功因为内存队列requestQueue中目前没有数据所以线程阻塞。其他两个同理。 请求制造操作 请求接口返回true 查看日志 可以发现飞机、轮船、汽车的制造都完成了并且和前面说的一样用时4s左右完美实现。 可能有人会发出疑问为何每个服务中的请求对象中都要单独定义CountDownLatch难道不可以定义一个全局的CountDownLatch数字设置为3就可以了每个线程完成了就减1就可以了其实这种方式也可以但考虑到实际业务中3个服务线程并不一定会被同时投递或者小明只单独订购飞机呢是吧所以对每个线程的请求对象单独进行维护CountDownLatch。 总结 在本文中我们深入探讨了如何优化一个复杂的API请求涉及多个独立业务模块且需要尽快返回结果的问题。通过结合多线程Runnable、CountDownLatch以及PriorityBlockingQueue我们实现了高效的并行处理和有序的任务管理成功地在当前接口返回了最终的结果。这一解决方案不仅提高了系统性能还确保了结果的及时返回具有很强的实用性和可操作性。希望本文提供的思路和实践能为大家在类似场景中提供有效的解决方案。
文章转载自:
http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn
http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn
http://www.morning.txlxr.cn.gov.cn.txlxr.cn
http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn
http://www.morning.rkrl.cn.gov.cn.rkrl.cn
http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn
http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn
http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn
http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn
http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn
http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn
http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn
http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn
http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn
http://www.morning.mlyq.cn.gov.cn.mlyq.cn
http://www.morning.bylzr.cn.gov.cn.bylzr.cn
http://www.morning.spdyl.cn.gov.cn.spdyl.cn
http://www.morning.npxcc.cn.gov.cn.npxcc.cn
http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn
http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn
http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn
http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn
http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn
http://www.morning.ylph.cn.gov.cn.ylph.cn
http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn
http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn
http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn
http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn
http://www.morning.jcwt.cn.gov.cn.jcwt.cn
http://www.morning.prmyx.cn.gov.cn.prmyx.cn
http://www.morning.jllnh.cn.gov.cn.jllnh.cn
http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn
http://www.morning.qstjr.cn.gov.cn.qstjr.cn
http://www.morning.wdskl.cn.gov.cn.wdskl.cn
http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn
http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn
http://www.morning.wpjst.cn.gov.cn.wpjst.cn
http://www.morning.ujianji.com.gov.cn.ujianji.com
http://www.morning.rgksz.cn.gov.cn.rgksz.cn
http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn
http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn
http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn
http://www.morning.spxsm.cn.gov.cn.spxsm.cn
http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn
http://www.morning.lskyz.cn.gov.cn.lskyz.cn
http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn
http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn
http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn
http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn
http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn
http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn
http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn
http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn
http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn
http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn
http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn
http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn
http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn
http://www.morning.rxlk.cn.gov.cn.rxlk.cn
http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn
http://www.morning.bbgn.cn.gov.cn.bbgn.cn
http://www.morning.hqllj.cn.gov.cn.hqllj.cn
http://www.morning.bljcb.cn.gov.cn.bljcb.cn
http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn
http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn
http://www.morning.spwln.cn.gov.cn.spwln.cn
http://www.morning.smrkf.cn.gov.cn.smrkf.cn
http://www.morning.yqgny.cn.gov.cn.yqgny.cn
http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn
http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn
http://www.morning.mkyny.cn.gov.cn.mkyny.cn
http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn
http://www.morning.kaweilu.com.gov.cn.kaweilu.com
http://www.morning.zrpys.cn.gov.cn.zrpys.cn
http://www.morning.ywrt.cn.gov.cn.ywrt.cn
http://www.morning.hphqy.cn.gov.cn.hphqy.cn
http://www.morning.rtbj.cn.gov.cn.rtbj.cn
http://www.morning.cwqln.cn.gov.cn.cwqln.cn
http://www.morning.clkjn.cn.gov.cn.clkjn.cn
http://www.morning.synkr.cn.gov.cn.synkr.cn
http://www.tj-hxxt.cn/news/261848.html

相关文章:

  • 专门做产品定制的网站做彩票交流网站犯法吗
  • 东莞专业网站建设平台云浮网站建设咨询
  • wordpress本地网站怎么访问wordpress安装主题连接不上ftp
  • 大型门户网站建设功能Wordpress个人套餐
  • 网站建设 环保 图片网络营销推广手段
  • 手机网站服务器天津建设厅网站首页
  • 各地农业信息网站的建设温州微信网站定制
  • 常见的网站结构有网站建设中怎么设置默认页
  • 苏州和城乡建设局网站首页常州网络推广seo
  • 做电商网站报价合肥门户网站建设
  • 广东省建设工程交易中心网站wordpress 充值卡
  • 有什么好的网站推荐一下58同城网站建设的不足
  • 什么网站可以做外链手机app推广联盟
  • 复兴区建设局网站怎样用织梦建设网站
  • 深圳外贸网站建设服务商软件开发报价单范本
  • 网站设计服务费一般多少钱网站做电子公章违法吗
  • 网站服务设计网站的排名与权重
  • 大型行业网站wordpress社区主题
  • 网站建设如何财务处理电脑培训学校
  • 上海个人建站龙岩正规招聘网
  • 汽修网站怎么做wordpress 按月归档
  • 网站seo优化管理系统wordpress改网站地址
  • 世界建筑设计网站网站建设公众号
  • 给人做网站赚钱吗社交做的最好的网站有哪些
  • 网站建设需要学什么证网站做多久流量
  • 工商登记网站网络代码
  • 网站单页面什么叫域名什么是域名
  • 那个网站做拍手比较好微信公众号免费模板网站
  • 域名备案关闭网站抓取工具把对手网站的长尾词
  • 网站系统繁忙是什么意思广州天府路一栋楼外墙脚手架坍塌