Is the pre-increment operator thread-safe?

前端 未结 5 619
孤城傲影
孤城傲影 2020-12-03 17:42

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.

5条回答
  •  难免孤独
    2020-12-03 18:07

    No, you should be using something like java.util.concurrent.atomic.AtomicInteger. Look at its getAndIncrement() method.

提交回复
热议问题