0

How can I rewrite my code without using concurrent collections?

public static void main(String[] args) {

ArrayList<Integer> source = new ArrayList<>();

for(int i = 0; i < 5; i++) {
    source.add(i);
}
List<Integer> synchList = Collections.synchronizedList(new ArrayList<>());

     Runnable runnable = () -> {synchList.addAll(source);}; 
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
  • 1
    Does this answer your question? [Correct way to synchronize ArrayList in java](https://stackoverflow.com/questions/1431681/correct-way-to-synchronize-arraylist-in-java) – Pouya Heydari Feb 23 '21 at 21:47
  • 1
    Just replace `List synchList = Collections.synchronizedList(new ArrayList<>( ));` with `List synchList = new ArrayList<>();`. In the code you’ve posted, there is no need for any synchronization. If you want to do something different, post the relevant code instead of a pointless snippet. – Holger Feb 24 '21 at 12:44

0 Answers0