How do I make my ArrayList Thread-Safe? Another approach to problem in Java? How do I make my ArrayList Thread-Safe? Another approach to problem in Java? multithreading multithreading

How do I make my ArrayList Thread-Safe? Another approach to problem in Java?


Change

private ArrayList finishingOrder;//Make an ArrayList to hold RaceCar objects to determine winnersfinishingOrder = Collections.synchronizedCollection(new ArrayList(numberOfRaceCars)

to

private List finishingOrder;//Make an ArrayList to hold RaceCar objects to determine winnersfinishingOrder = Collections.synchronizedList(new ArrayList(numberOfRaceCars)

List is a supertype of ArrayList so you need to specify that.

Otherwise, what you're doing seems fine. Other option is you can use Vector, which is synchronized, but this is probably what I would do.