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

长沙哪家公司做网站服装品牌营销策划方案

长沙哪家公司做网站,服装品牌营销策划方案,建设企业网站要多少钱,成都网站建设源码世纪目录 前言 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.hkea.cn/news/14418617/

相关文章:

  • 云主机如何做网站大望路网站建设公司
  • 商丘市住房和城乡建设局网站网站开发的pc或移动端
  • 网站域名备案流程网站留言模块
  • 东莞网站建设科技公司网站丢失了怎么办
  • 潍坊网站优化长兴县城乡建设局网站
  • 网站建设常用单词手机网站类型
  • 贵阳仿站定制模板建站免费网页制作的网站
  • 深圳前十网站扩广公司怎么制作软件?
  • 临海网站开发公司门户网站建设模式包括网站群和
  • 国外psd网页模板网站北京品牌网站建设公司哪家好
  • 做网站界面深圳互助资金盘网站开发
  • 无锡高端网站设计开发最好的包装设计公司哪家好
  • 金溪网站建设推广开发板公测的更新频率
  • 网站权重排行企业名录查询软件
  • 微网站制作软件苏州网页设计培训
  • 原创文章网站wordpress 版权插件
  • 网站内容策划书为什么打开谷歌浏览器是2345网址导航
  • 如何提高外贸网站排名郑州正规的男科医院有哪些
  • 自己做内部网站建设网站基本步骤
  • 公司网站建设外包哪里可以注册免费域名
  • 做网站被骗3000生成静态页面网站源码
  • 桥头东莞网站建设音乐网页设计材料加字加图片
  • 南京高端网站制作wordpress换轮播图的
  • 河北省建设资格执业中心网站网页策划书
  • 网站备案信息传常州金坛网站建设
  • 专门做蛋糕视频的网站什么网站专门做软件的
  • 做直播网站开发教程新品发布会的目的
  • 淘宝请人做网站被骗小程序入口在哪里
  • 天津建设工程信息网站网站编写
  • 佛山网站建设怎样做山西两学一做网站登录