Race conditions “check-then-act” and “read-modify-

2020-02-13 00:55发布

Anyone can explain me about what is race condition, how to avoid it, and how to find it out in java codes?

Okay, I just know "race condition" several days, I have two examples, maybe they are not good enough, that's why I need your help:) Hope any of you can explain it for me.

example1: check then act:

if(vector.contains(e))//check
{
vector.remove(e)
}

if there are 2 threads can access, thread1 suspends after check vector contains e, and e does in vector, then thread2 access to check and then remove e from vector, then thread1 comes back and do remove action, error will occur, because e is already removed by thread2.

example2: read modify write:

assume we have a counter variable in a method, once the method is called, counter increase 1,

counter++

this is not a atomic operation, it has 3 steps: 1. get the value 2. increase the value 3. assign to the value

What I know about race condition is all here, hope you can share your knowledge with me:)

thanks

3条回答
对你真心纯属浪费
2楼-- · 2020-02-13 01:31

What is a race condition? Check this stack-overflow question.

There are primarily two scenarios for race-condition: read-modify-write and check-then-act.

For read-modify-write classical example is of counter++ which is not an atomic operation so leads to race condition.

For check-then-act there are multiple examples. One example is when you check for key existence in ConcurrentHashMap and then do some work in if-case. Another example is singleton class code:

public Singleton getInstance()
{
   if(_instance == null)
   { 
      _instance = new Singleton();
   }
}

You can read more about them on internet. One good book on concurrency is Java Concurrency in Practice by Brian Goetz. You can also find this article helpful.

查看更多
小情绪 Triste *
3楼-- · 2020-02-13 01:35

The examples provided in the answer: "read-modify-write" and "check-then-act" are Not Sufficient to define cases of race condition.

If you understand a 'race condition' as a condition of unpredictable result caused by two or more threads accessing shared memory in undetermined order, where at least one access is for write, then (a) you do not need to have both 'read' and 'write' in the same thread.

And (b) the atomic definition of the variable does not save us from a potential of a race condition, either. If one thread writes to atomic variable before of after the other thread reads from it and the accesses are not ordered correctly, the results would still be unpredictable. You can see further explanation of this point here.

查看更多
趁早两清
4楼-- · 2020-02-13 01:43

From Java Concurrency in Practice book

"A race condition occurs when the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, when getting the right answer relies on lucky timing"

The most common condition of race condition is check-then-act, where a potentially safe observation is used to make decision on what do next.

Another common condition of race condition put if absent

For your example 1 .

 if (!vector.contains(element))
   vector.add(element);

This attempt at a put-if-absent operation has a race condition, even though contains and add are atomic. While synchronized methods can make individual operations atomic, additional locking is required - when multiple operations are combined into compound action.

查看更多
登录 后发表回答