| 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
|
 |
 |
|
|
subject: Threads again
|
|
|