I will be getting a JSON string every .01 seconds or even faster and I have to insert it in some collection so that I can later loop through every row and do some processing and delete that row after that. The process of insertion keeps on going. I am a bit confused as whether to use ArrayList
or Queue
.
So basically I would insert the first message then second and then ... and simultaneously another process would read the first inserted value for processing and deleting that record followed by second inserted value. Please advise which one would be fastest and takes less storage? Or any other collection would best suit my requirement?
Edit: I have two methods -
GetMessages() - which keeps on getting/adding messages in queue (here the messages are received from only one source which keeps on sending messages and does not stop till we manually stop the process)
ProcessMessages() - which will read the queue records and after processing delete them from the queue
Saving object in java can be accomplished in a number of ways, the most prominent of which is to serialize the object - saving it to a file and reading the object using an ObjectOutputStream and ObjectInputStream, respectively. Serialization is a fantastic advantage to using java.
The utility package, (java. util) contains all the classes and interfaces that are required by the collection framework.
The java. util package contains the Collections class. Collections class is basically used with the static methods that operate on the collections or return the collection. All the methods of this class throw the NullPointerException if the collection or object passed to the methods is null.
Sounds like you should use a ConcurrentQueue<string>
, where one thread can push values that another thread could pop.
Definitely don't use or even think of using the pre-generic ArrayList
type ever again.
You'd still need some bookkeeping code that checks whether your processing is running faster than the insertion, otherwise you're guaranteed to run out of memory at some point.
It seems that you are looking for a Producer-Consumer design pattern (Wikipedia Producer–consumer_problem).
If it's your case, try BlockingCollection which is specially designed for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With