aspose file tools*
The moose likes Mock Exam Errata and the fly likes jtips exam#1, question 58 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "jtips exam#1, question 58" Watch "jtips exam#1, question 58" New topic
Author

jtips exam#1, question 58

Rosie Vogel
Ranch Hand

Joined: Jan 31, 2001
Posts: 228
By all means correct me if I am wrong but it seems to me there is a mistake here.

The author says that the output cannot be determined. But surely the output can only ever be ten times the word "Sun"? Because the thread never gets to run and even if it did, I don't see how it could influence the output. Do you agree?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I agree with the author. The only thing you can say for sure is that some string will get printed 10 times and it can be any mix of "Moon" or "Sun". But you can say that once it switches to "Moon" it will stay as "Moon"
Reasoning:
You start the thread and this will schedule it with the thread schedular. Then either the thread that changes the string will start or the thread that prints out the strings will start. But there is no way you will know which will go first. So it may change the value so you get 10 "Moon"s or it may do the loop first so you get 10 "Sun"s. Or it may go half way so you get 5 "Sun"s and then 5 "Moon"s.
Does that make sense?
Bill
Rosie Vogel
Ranch Hand

Joined: Jan 31, 2001
Posts: 228
Hello Bill,
Thanks for your reply. I still don't completely understand. There is no "while true" or other while-clause, can the thread run without one?
Rajesh R
Greenhorn

Joined: Mar 02, 2001
Posts: 12
Hi,
Here, there are two threads, One thread called "main", which belongs to the ThreadGroup "main", executing the main method. Another one(your thread), executing the run() method.
You know that the threads keeps the working copies of variables that resides in main memory. "Your thread" does an "assign" to s variable and the "main" thread does a "use" of s variable. As these thread are running concurrently, it can not be predicted whether "use" or assign will take place first.
Assume that "use" took place first, then "main" thread is now having value "Sun" and this keeps printing "sun". But before this method finishes, "your thread" might have assigned a value "moon" to s. If this assignment was the first to take place, then main thread would have started printing "moon" instread of "sun"
Again it is not certain that "sun" or "moon" will be printed 10 times. During the execution the "main" thread( which started with printing "sun"), might have reconciled its working copy of variable s with the main memory, then it will start printing "moon" from then onwards.
The best way to avoid this kind of situation is to syncronize. But this does only gurantee that either "moon" or "sun" will be printed 10 times. You cannot be sure which one!
Please let me know what you think.
thanks,
Rajesh
Rosie Vogel
Ranch Hand

Joined: Jan 31, 2001
Posts: 228
Hello Rajesh,
Thanks for your reply. I understand what you are saying about the working of threads.
But I am still not sure whether a thread gets to run without some kind of while-clause (like "while true") in the run method. I have tried this and it doesn't seem to work. That is why I thought only the main thread would run.
Your comments please?
Rosie Vogel
Ranch Hand

Joined: Jan 31, 2001
Posts: 228
Please just ignore my last question. I have just realized I was talking nonsense.
 
 
subject: jtips exam#1, question 58