加入收藏 | 设为首页 | 会员中心 | 我要投稿 大同站长网 (https://www.0352zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

鸟瞰 Java 并发框架

发布时间:2019-07-18 14:31:20 所属栏目:优化 来源:唐尤华
导读:副标题#e# 1. 为什么要写这篇文章 几年前 NoSQL 开始流行的时候,像其他团队一样,我们的团队也热衷于令人兴奋的新东西,并且计划替换一个应用程序的数据库。但是,当深入实现细节时,我们想起了一位智者曾经说过的话:细节决定成败。最终我们意识到 NoSQL

Disruptor 框架在下列场合性能更好:与事件驱动的体系结构一起使用,或主要关注内存任务的单个生产者和多个消费者。

  1. static { 
  2.     int userId = new Random().nextInt(10) + 1; 
  3.  
  4.     // 示例 Event-Handler; count down latch 用于使线程与 http 线程同步 
  5.     EventHandler<Event> postsApiHandler = (event, sequence, endOfBatch) -> { 
  6.         event.posts = JsonService.getPosts(); 
  7.         event.countDownLatch.countDown(); 
  8.     }; 
  9.  
  10.     // 配置 Disputor 用于处理事件 
  11.     DISRUPTOR.handleEventsWith(postsApiHandler, commentsApiHandler, albumsApiHandler) 
  12.     .handleEventsWithWorkerPool(photosApiHandler1, photosApiHandler2) 
  13.     .thenHandleEventsWithWorkerPool(postsAndCommentsResponseHandler1, postsAndCommentsResponseHandler2) 
  14.     .handleEventsWithWorkerPool(albumsAndPhotosResponseHandler1, albumsAndPhotosResponseHandler2); 
  15.     DISRUPTOR.start(); 
  16.  
  17. // 对于每个请求,在 RingBuffer 中发布一个事件: 
  18. Event event = null; 
  19. RingBuffer<Event> ringBuffer = DISRUPTOR.getRingBuffer(); 
  20. long sequence = ringBuffer.next(); 
  21. CountDownLatch countDownLatch = new CountDownLatch(6); 
  22. try { 
  23.     event = ringBuffer.get(sequence); 
  24.     event.countDownLatch = countDownLatch; 
  25.     event.startTime = System.currentTimeMillis(); 
  26. } finally { 
  27.     ringBuffer.publish(sequence); 
  28. try { 
  29.     event.countDownLatch.await(); 
  30. } catch (InterruptedException e) { 
  31.     e.printStackTrace(); 

10. Akka

鸟瞰 Java 并发框架

图片来自:https://blog.codecentric.de/en/2015/08/introduction-to-akka-actors/

  • Akka 库的主要优势在于它拥有构建分布式系统的本地支持。
  • 它运行在一个叫做 Actor System 的系统上。这个系统抽象了线程的概念,Actor System 中的 Actor 通过异步消息进行通信,这类似于生产者和消费者之间的通信。
  • 这种额外的抽象级别有助于 Actor System 提供诸如容错、位置透明等特性。
  • 使用正确的 Actor-to-Thread 策略,可以对该框架进行优化,使其性能优于上表所示的结果。虽然它不能在单个节点上与传统方法的性能匹敌,但是由于其构建分布式和弹性系统的能力,仍然是首选。

10.1 示例代码

  1. // 来自 controller : 
  2. Actors.masterActor.tell(new Master.Request("Get Response", event, Actors.workerActor), ActorRef.noSender()); 
  3.  
  4. // handler : 
  5. public Receive createReceive() { 
  6.     return receiveBuilder().match(Request.class, request -> { 
  7.     Event event = request.event; // Ideally, immutable data structures should be used here. 
  8.     request.worker.tell(new JsonServiceWorker.Request("posts", event), getSelf()); 
  9.     request.worker.tell(new JsonServiceWorker.Request("comments", event), getSelf()); 
  10.     request.worker.tell(new JsonServiceWorker.Request("albums", event), getSelf()); 
  11.     request.worker.tell(new JsonServiceWorker.Request("photos", event), getSelf()); 
  12.     }).match(Event.class, e -> { 
  13.     if (e.posts != null && e.comments != null & e.albums != null & e.photos != null) { 
  14.     int userId = new Random().nextInt(10) + 1; 
  15.     String postsAndCommentsOfRandomUser = ResponseUtil.getPostsAndCommentsOfRandomUser(userId, e.posts, 
  16.     e.comments); 
  17.     String albumsAndPhotosOfRandomUser = ResponseUtil.getAlbumsAndPhotosOfRandomUser(userId, e.albums, 
  18.     e.photos); 
  19.     String response = postsAndCommentsOfRandomUser + albumsAndPhotosOfRandomUser; 
  20.     e.response = response; 
  21.     e.countDownLatch.countDown(); 
  22.     } 
  23.     }).build(); 

(编辑:大同站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!