简体   繁体   English

高效的排序数据结构,可以存储原始消息以及相应的时间戳

[英]Sorted Data Structure which is efficient and can store the original message with the respective timestamps

I want to send different XML message strings based on the time. 我想根据时间发送不同的XML消息字符串。 I have thought of implementing a data structure in JAVA and put all the XML messages in it with the time when they should be sent out. 我考虑过要在JAVA中实现数据结构,并在所有XML消息发送出去的时间里放入其中。 I want to use sorted data structure so that every time the message comes inside the data structure, it gets sorted and while sending out I can check their respective times and take the messages out. 我想使用排序的数据结构,以便每当消息进入数据结构中时,消息都会被排序,并且在发送时我可以检查它们各自的时间并取出消息。 I want to know which data structure would be most efficient and how can I implement it. 我想知道哪种数据结构将是最有效的,以及如何实现它。 Also how can I put time in seconds with each XML string. 另外,如何将每个XML字符串的时间以秒为单位。

It is like: 它像是:

Message 1, time to send T + 1  ---- put to the data structure
Message 2, time to send T + 4  ---- put to the data structure
Message 3, time to send T + 3  ---- put to the data structure....

There will be a separate thread that will check the messages in the data structure every second and remove all the messages which qualifies to come out. 将有一个单独的线程,它将每秒检查一次数据结构中的消息,并删除所有符合条件的消息。

If I understood your problem properly. 如果我正确理解您的问题。 You want 你要

  1. To store messages which will be sorted according to time 存储将根据时间排序的消息
  2. will retrieve message. 将检索消息。 The message with earlier timestamp will be retrieved first 时间戳较早的消息将首先被检索
  3. will be accessed by multiple thread.. 将被多个线程访问。

If The above cases ae right then I think you will need a thread-safe queue. 如果上述情况正确,那么我认为您将需要一个线程安全队列。 Because 因为

  1. As you need to sort based on time so no need to sort manually. 由于您需要根据时间进行排序,因此无需手动进行排序。 as message with earlier time will be inserted first 因为时间较早的消息将首先插入
  2. it is a fifo. 这是一个fifo。

In this case the ava.util.concurrent.ConcurrentLinkedQueue will be a good option 在这种情况下, ava.util.concurrent.ConcurrentLinkedQueue将是一个不错的选择

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

相关问题 使用哪种数据结构按顺序存储条目及其时间戳 - which data structure to use to store the entries with their time stamp in sorted order 排序的类似地图的数据结构,允许重复并可以存储到文件 - Sorted maplike data structure which allows duplicates and can be stored to file 可以使用迭代器排序的数据结构,以保持插入顺序 - Data structure which can be sorted with iterator maintaining insertion order Sorted Array vs Hashtable:在日历应用程序中搜索一系列日期时,哪种数据结构更有效? - Sorted Array vs Hashtable: Which data structure would be more efficient in searching over a range of dates in a calendar app? 使用高效的添加和索引获取方法对Java数据结构进行排序 - Sorted java data structure with efficient add and indexed get method 高效的数据结构可存储数百万条记录 - Efficient Data Structure To Store Millions of Records 如何以排序形式存储 int 数组的原始索引? - how can i store the original index of an int array in sorted form? Java中的可排序有效迭代排序结构 - Sorted Iterable Efficient Sorted Structure in Java Java中的哪个数据结构可以在恒定时间添加键/值对并保持按值排序? - which data structure in java can add key/value pair at constant time and maintain sorted by value? 存储共同作者的数据结构是什么? - Which data structure to store co-authorship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM