This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Race Condition

 
Manjunatha Kampli Kottal
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain when the race condition occurs? is it because of the way thread scheduling works or because of the bad programming? i think its because of thread scheduling mechanism but not sure.please clarify
 
Prabakar Kalivaradan
Greenhorn
Posts: 20
Chrome Eclipse IDE Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Race Condition: The possibility of incorrect results in the presence of unlucky timing; the correctness of a computation depends on the relative timing or interleaving of multiple threads by the runtime; in other words, getting the correct result depends on chance factor;
>The most common type of race condition is check-then-act, where a potentially stale observation is used to make a decision on what to do next

1. if(obj == null)
2. obj = new OnlyOne();

In the above example, say you don't want to create more than one OnlyOne() object; if thread A comes to line 1 then moves to line 2 because obj is null; at the same time if thread B enters line 1 before thread A creates OnlyOne(), thread B also will see that obj is null; so both will end up creating their own OnlyOne objects; so there are two objects now which you didn't want.

This is called race condition.
 
Prabakar Kalivaradan
Greenhorn
Posts: 20
Chrome Eclipse IDE Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically Race condition occurs due to poor design (concurrent design).
 
Manjunatha Kampli Kottal
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Prabakar Kalivaradan
Thanks for the example. so you mean to say race condition is because of the poor design. and by properly synchronizing the code we can avoid the race condition right?
just like
synchronized(this)
{
if(obj == null)
obj = new OnlyOne();
}
 
Prabakar Kalivaradan
Greenhorn
Posts: 20
Chrome Eclipse IDE Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right!

with the right design and implementation that follows that right design such conditions can be avoided.

it becomes harder to rectify after the implementation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic