Synchronized lists
Hey guys!
A bit of concurrency question here. I was using
CopyOnWriteArrayList to have a threadsafe list that I can write to from different threads. The whole purpose was that I need to make a multitude ap API calls, process the information and save it to a collection. The API calls could be run concurrently, so I would make the process faster, so I needed a threadsafe implementation of List. I found that CopyOnWriteArrayList is such an implementation, but it wasn't working well. I know how many items I need to have in the collection after finishing, and that amount wwas always lower. I tried then using Collections.synchronizedList() which had a better result, but still not the amount I needed. When I did the process sequentially, then I got the exact number of items I needed. So something is wrong with synchronization, or is there something I need to know, that I am not aware of synchronized lists in Java fro them to be truly threadsafe? Coding wise I initialized the array with Collections.synchronizedList(), and used virtual threads for the API calls, in which the writing is done.
A sample of what I did:
Thank you for the answers in advance!8 Replies
⌛ This post has been reserved for your question.
Hey @Glayer! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
I don't see you actually using your
executor.
You need to submit your tasks to the executor with execute() and then wait in your main task until all tasks have finished such that the list has been filled.
You e.g. use ExecutorService.invokeAll() and wait for all Futures to be true for isDone().Using virtual or native threads is irrelevant to the problem of a synchronized collection.
A synchronized list will lock the entire list for every thread, while a concurrent list (like CopyOnWriteArrayList) will lock only parts of it and will allow multiple reads at once.
See
* baeldung
* geeksforgeeks
Baeldung on Kotlin
An Introduction to Synchronized Java Collections | Baeldung
Learn how to create synchronized collections using the static synchronization wrappers available in the Java Collections Framework.
GeeksforGeeks
Difference Between Synchronized ArrayList and CopyOnWriteArrayList ...
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.Note:
CopyOnWriteArrayList is made for situations where you read a lot from the list but modifications are rare.
Both CopyOnWriteArrayList and Collections.synchronizedList() give you thread safe Lists. What exactly wasn't working for you?
CopyOnWriteArrayList doesn't completely lock the collection during write operations - other threads can still read from itI agree with @Madjosz | codewars.com : You're probably not properly waiting for the threads to finish their work. When you're farming work off to multiple threads, you have wait for all them to complete. If you try to read from your results list before they've all finished, you'll get invalid/inconsistent results. To re-iterate, add each API call as a task to the executor, then
shutdown() & awaitTermination() on it.the try-with-finally does that
but if the list isn't modified from multiple threads, the list doesn't need to be thread-safe
but the code provided is inconclusive
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.