Originally posted by Roshini Chandrasekar: I am a beginner to java completely. Soo please don't think any bad if this is real silly. I am thinking the answer would be y=2. Is it correct??
It can actually be 0, 1, or 2. If thread 1 writes and then thread 3 reads, then it will be 1. If thread 2 writes and then thread 3 reads, then it will be 2. If thread 3 reads before either thread 1 or 2 gets to it, then it will be 0. (The caches will also have an affect on the outcome too)
Ah, the glory of running threads! It's just part of the deal that you can't know ahead of time in what order different threads will do things. You can go to a lot of effort to force them into some sequence, but if you want sequential operations, you probably don't want threads.
Do you have a real business problem to solve, or is "We cannot know" a valid answer to a quiz? If it's a real situation, let's dig into more details and see if we can help.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 24, 2007
Thanks Henry and Stan.. Guess what I thought is correct It's not about real time application actually.. Just started threads in java and going through some example problems.