Friday, July 6, 2012

Race Condition vs Deadlock

Race Condition & Deadlock are not the same. Please refer following examples to understand the differences.
 
Race Condition: When two threads randomly tries to update a common variable.
 
Eg: 

  1. Var1 = 100
  2. Thread T1 & Thread T2 reads Var1 at same time
  3. But, T2 updates Var1 to 120 & exits
  4. Then, T1 updates Var1 to 150 & exits
  5. This leads to confused result for Var1
[Expected result is 100 + 20 + 50 = 170]

 
Dead Lock: Two thread waiting without end
 

Eg:
  • Var1 is locked & used by Thread T1.
  • Var2 is locked & used by Thread T2.
  • T1 waits for Var1 to be released & T2 waits for Var2 to be released leading to dead lock.
http://support.microsoft.com/kb/317723

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete