What is a race condition?

后端 未结 18 2981
谎友^
谎友^ 2020-11-21 04:52

When writing multithreaded applications, one of the most common problems experienced is race conditions.

My questions to the community are:

What is the rac

18条回答
  •  萌比男神i
    2020-11-21 05:22

    You can prevent race condition, if you use "Atomic" classes. The reason is just the thread don't separate operation get and set, example is below:

    AtomicInteger ai = new AtomicInteger(2);
    ai.getAndAdd(5);
    

    As a result, you will have 7 in link "ai". Although you did two actions, but the both operation confirm the same thread and no one other thread will interfere to this, that means no race conditions!

提交回复
热议问题