This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Race Condition

  • 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
  • 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
Posts: 20
Google Web Toolkit Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically Race condition occurs due to poor design (concurrent design).
 
Manjunatha Kampli Kottal
Posts: 6
  • 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
Posts: 20
Google Web Toolkit Eclipse IDE Chrome
  • 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.
 
Those are the largest trousers in the world! Especially when next to this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!