Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Welcome to JavaRanch! TellTheDetails please, otherwise we can't help you! There is no concept called Race - Condition in Java Threads.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
 

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.
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 725 times.
Similar Threads
Cleared SCJP with 86%
Passed SCJP 6.0 75%
Taking SCJP in September. Should I go for 1.4 or 1.5?
Passed SCJP 1.4 (70%)
How to fail in SCJP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:06:44.