2
votes

I want to send different XML message strings based on the time. 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. 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.

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.

1

1 Answers

1
votes

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.

In this case the ava.util.concurrent.ConcurrentLinkedQueue will be a good option