Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need understanding about Fig.9-4 in the K&B SCJP 6 book

 
H Ejilane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was going through the SCJP 6 book by Kathy & Bert as part of my SCJP 6 preps. I came across this Figure 9-4 in the Threads section (Chapter 9) of this book. Here the author talk about the concept of 'Race-condition' between threads.

I tried to understand how this illustration exemplifies the idea of a race-condition but in vain. I would greatly appreciate if any of you could throw some light on this figure.

Appreciate your help,
Hari
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch! TellTheDetails please, otherwise we can't help you! There is no concept called Race - Condition in Java Threads.
 
H Ejilane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me phrase it this way.

I found the following para in Kathy & Bates' SCJP Sun Certified Programmer for Java 6 Exam 310-065 book.

Reference :

Page no. 733,
Chapter : 9 - Threads,
Section : Synchronizing code


The passage goes like this,

The problem is known as "race condition," where multiple threads can access the same resource (typically an object's instance variables), and can produce corrupted data if one thread "races in" too quickly before an operation that should be "atomic" has completed.

Now starts my question. Just above this para there is a figure that illustrates a race-condition scenario. So Im not able to correlate this picture with the above definition. So I was just wondering if someone on the floor had an understanding and would share his thoughts.

Hope this gives an clearer understanding of my problem.
 
See Furst
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Welcome to JavaRanch! TellTheDetails please, otherwise we can't help you! There is no concept called Race - Condition in Java Threads.


There is one in computer science...Then again Java doesn't even understand the concept of 0xFF without turning it into an integer first. Go figure.
 
See Furst
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Ejilane wrote: Ok, let me phrase it this way.

I found the following para in Kathy & Bates' SCJP Sun Certified Programmer for Java 6 Exam 310-065 book.

Reference :

Page no. 733,
Chapter : 9 - Threads,
Section : Synchronizing code


The passage goes like this,

The problem is known as "race condition," where multiple threads can access the same resource (typically an object's instance variables), and can produce corrupted data if one thread "races in" too quickly before an operation that should be "atomic" has completed.

Now starts my question. Just above this para there is a figure that illustrates a race-condition scenario. So Im not able to correlate this picture with the above definition. So I was just wondering if someone on the floor had an understanding and would share his thoughts.

Hope this gives an clearer understanding of my problem.


First of all you have to understand that the main problem with the diagram is that it doesn't really represent time too well...
Imagine that instead of next to each other.. the threads are one above the other as time progresses from left to right. Now imagine that at one point in time A and B are pointing to object one... At the next position in time A points to two and B point to one and then the next A and B are both pointing to object two... The race condition occurs between time periods two and three where B does something to object two that A depended on before A was finished with the data in object two, but B "raced in" and changed things before A was done with it's operation. This means that A is dealing with corrupt data.

There is a better example with StringBuffer on page 742 in the Thread Safe Classes section.

 
H Ejilane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Furst wrote:
H Ejilane wrote: Ok, let me phrase it this way.

I found the following para in Kathy & Bates' SCJP Sun Certified Programmer for Java 6 Exam 310-065 book.

Reference :

Page no. 733,
Chapter : 9 - Threads,
Section : Synchronizing code


The passage goes like this,

The problem is known as "race condition," where multiple threads can access the same resource (typically an object's instance variables), and can produce corrupted data if one thread "races in" too quickly before an operation that should be "atomic" has completed.

Now starts my question. Just above this para there is a figure that illustrates a race-condition scenario. So Im not able to correlate this picture with the above definition. So I was just wondering if someone on the floor had an understanding and would share his thoughts.

Hope this gives an clearer understanding of my problem.


First of all you have to understand that the main problem with the diagram is that it doesn't really represent time too well...
Imagine that instead of next to each other.. the threads are one above the other as time progresses from left to right. Now imagine that at one point in time A and B are pointing to object one... At the next position in time A points to two and B point to one and then the next A and B are both pointing to object two... The race condition occurs between time periods two and three where B does something to object two that A depended on before A was finished with the data in object two, but B "raced in" and changed things before A was done with it's operation. This means that A is dealing with corrupt data.

There is a better example with StringBuffer on page 742 in the Thread Safe Classes section.





Thanks See furst... You've showed me the light at the end of the tunnel.

The Thread Safe Classes section that you pointed out gave me a better understanding of race-conditions.

I appreciate your help very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!