简体   繁体   English

通过不同的类同时处理对象列表的最佳方法是什么

[英]What is the best way to process a object list at the same time by different classes

I am trying to process a object list at the same time with different classes, but I am not sure if I am heading in the right direction. 我正在尝试同时使用不同的类处理对象列表,但是我不确定是否朝着正确的方向前进。 I have read up about ExecutorService and Parallel stream, but not sure if it is the correct way. 我已经阅读了有关ExecutorService和Parallel流的信息,但不确定它是否正确。

So to provide an example: 所以举个例子:

I have a publisher that collects data and places it in a list. 我有一个收集数据并将其放置在列表中的发布者。 The publisher has multiple subscribers linked to it, which need to process the data and store it in a map. 发布者有多个与其链接的订户,这些订户需要处理数据并将其存储在地图中。 The map is then built up until all the data is processed and is then stored in a database table. 然后构建映射,直到处理完所有数据,然后将其存储在数据库表中。 Each subscriber has their own table that needs to be populate in some form with the data provided. 每个订户都有自己的表,该表需要以某种形式填充所提供的数据。 What I am trying to accomplish is distributing the list to the subscribers at the same time and once all subscribers are finished processing the next set off data is supplied and so forth until all the data has been processed for a date range. 我要完成的工作是同时将列表分发给订户,一旦所有订户都完成了处理,就提供下一个抵销数据,依此类推,直到在日期范围内处理完所有数据为止。

If anyone has some suggestions what I can look at, that would be awesome. 如果有人对我的建议有什么建议,那将很棒。

I'd start this way: 我将以这种方式开始:

  • Publisher 发行人
    • do not collect to a list, rather make it an Observable and have all the subscribers observe him. 不要收集到列表中,而是使其成为可观察的,并让所有订阅者都对其进行观察。
    • On each batch, initialise a CountDownLatch to the batch size, and wait until it clears you. 在每个批次上,将CountDownLatch初始化为批次大小,然后等待其清除您。
  • Subscriber 订户
    • should observe events of the type they are interested in in a different thread (only one per type) 应该在不同的线程中观察他们感兴趣的类型的事件(每种类型仅一个)
    • and on finishing with the handling of an event - they should notify the CDL of the Publisher. 并且在完成事件处理后-他们应通知发布商CDL。
  • Publisher 发行人
    • after all the events are finished, and the CDL is released, should save the results to db 在所有事件完成并释放CDL之后,应将结果保存到db
    • and then go on to the next batch 然后继续下一批

Now, all this is a suggestion, with multiple other possible solutions, and a very high-level one at that with many implementation details left unsaid. 现在,所有这一切都是一个建议,并提出了其他多种可能的解决方案,而其中一个非常高级的建议是,其中未提及许多实现细节。 You should keep your eyes open as you work, and not be afraid to change routes if it is better. 您在工作时应该睁大眼睛,如果情况更好,不要害怕改变路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 java 中的不同类调用相同方法的最佳方法 - Best way to call same method from different classes in java 填充列表的最佳方法是什么 <Object> ? - What's the best way to populate List<Object>? 将不同的JAVA类合并到一个类中的最佳方法是什么? - what is the best way to combine different JAVA classes in one single class? 在不同的JUnit测试类之间共享数据的最佳方法是什么 - What is the best way to share data between different JUnit test classes 对测试类使用相同的创建对象方法的最佳实践是什么 - What is the best practice for using the same create object methods for the test classes 在 Java 8 中设计/处理不同方法的有序调用的最佳方法是什么 - What is the best way to design/process in Java 8 ordered calls on different methods 什么是在Firebase Android中使用同一对象列表保存对象的最佳方法 - Whats the best way to save an object with a list of the same object in firebase android 用不同的RemoteWebDrivers驱动相同的JUnit类的最佳方法是什么? - What's the best way to drive same JUnit Class with different RemoteWebDrivers? 在Java中将List <Long>对象转换为long []数组的最佳方法是什么? - What is the best way of converting List<Long> object to long[] array in java? 将Object ArrayList转换为Double List的最佳方法是什么 - What would be the best way of converting Object ArrayList to Double List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM