Reputation: 635
Task is a simple. I have a dozen threads and one "global" list (of some objects). Each thread (periodically) iterate through all list to find the desired object and change it (or add it if not present). And then iterate again and save all object to the file. The JavaDoc says: "It is imperative that the user manually synchronize on the returned list when iterating over it" And now - all that I do with my list I do inside synchronized block. Inside implementation of synchronizedList, as I understand, also present some synchronization, and they (I suppose) add undesirable delay.
What if I will use simple List? I think - if all my doing with the list already in critical section - what I will lose if I change
private static final List<JobSummary> SyncJS = Collections.synchronizedList(new ArrayList<JobSummary>());
to
private static final List<JobSummary> SyncJS = new ArrayList<>();
Or I miss something?
Upvotes: 0
Views: 100
Reputation: 608
The operation "find the desired object and change it" is not atomic, hence you need to synchronize the list before performing any non atomic actions.
If you synchronize the list for all of your operations, then you do not need Collections.synchronizedList(new ArrayList<JobSummary>())
. new ArrayList<>()
would work well.
Upvotes: 0
Reputation: 4237
The point of the synchronizedObjects is to avoid manually synchronizing. If you are inside a synchronized
block then you don't need them. It will only be extra overhead.
Upvotes: 1