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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…