How do I make my ArrayList Thread-Safe? Another approach to problem in Java?
Use Collections.synchronizedList()
.
Ex:
Collections.synchronizedList(new ArrayList<YourClassNameHere>())
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.