I need help in ArrayList. I have an arraylist of strings. I am looping through this list and sending them to the output stream one after the other. While I am looping through the list and sending them, it is possible that another thread will be adding some elements to it. After an element is sent, it must be removed from the list as well.
How can I achieve this successfully?
If the items are added to the end of the list, you would better use a Queue instead. There are various thread-safe implementations available in Java5 and above, including ConcurrentLinkedQueue and LinkedBlockingQueue.
In general, the former is the preferable choice, unless you want a bounded blocking queue, in which case use the latter.
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