I\'m making a program in java that races a few cars against each other. Each car is a separate thread.
When cars complete the race, each one all calls this method.
Pre-increment on int is not thread safe, use AtomicInteger which is lock-free:
int
AtomicInteger
AtomicInteger carsComplete = new AtomicInteger(); //... switch(carsComplete.incrementAndGet())
BTW the code below is not thread safe as well. Can you tell why?
carsComplete.incrementAndGet(); switch(carsComplete.get())