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);}; 
}
2
  • 1
    Does this answer your question? Correct way to synchronize ArrayList in java Commented Feb 23, 2021 at 21:47
  • 1
    Just replace List<Integer> synchList = Collections.synchronizedList(new ArrayList<>( )); with List<Integer> 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. Commented Feb 24, 2021 at 12:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.