This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads again Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Threads again" Watch "Threads again" New topic
Author

Threads again

sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Hi all,

Here is a question:
What is the result of the code:

Answer given:
The progam prints pairs of values for x and y that are always the same on the same line)for example "x=1,y=1". In addition, each value appears only for once(for example "x=1,y=1" followed by "x=2,y=2").
There is another option among the answers:
The program prints pairs of values for x and y that might not always be the same on the same line(for example "x=2, y=1").
Why this option could not be a solution.
I mean, since there is no synchronization betweent he two threads, how can it be garunteed that the pairs of values for x and y will always be same.
Someone please clarify this.
Thx,
bye
sk
[ edited to turn off smilies -ds ]
[ April 05, 2004: Message edited by: Dirk Schreckmann ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
You're right, Sai. There is no guarantee that the two values will have the same value on every line. In fact, I modified the code slightly to make the run() method look like this:

I put in the extra for loop to increase the chance of a thread losing its time on the processor between the two increment statements and you can see the effect. If one thread increments x and then leaves the running state, the other thread can increment both x and y and you're left with two different values on the same line.
I hope that helps,
Corey


SCJP Tipline, etc.
sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Thx a lot corey,
You are doing a great job. I appreciate your help a lot.
Thx again.
bye
sk
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Threads again