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.
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 ]
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