File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Issue with Thread question in Marcus Green Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Issue with Thread question in Marcus Green" Watch "Issue with Thread question in Marcus Green" New topic
Author

Issue with Thread question in Marcus Green

Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
Here is the question:
Under what circumstances might you use the yield method of the Thread class
1) To call from the currently running thread to allow another thread of the same or higher priority to run
2) To call on a waiting thread to allow it to run
3) To allow a thread of higher priority to run
4) To call from the currently running thread with a parameter designating which thread should be allowed to run

The answer is 1 only, because, as the answer key wrote, (3) "but there is no guarantee that the thread that grabs the cpu time will be of a higher priority." But perhaps it is just bad wording of the question then. Because doesn't answer (1) entail that a yield() may be called to allow a thread of higher priority to start, even if that won't always happen. What do you guys think?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I think yield() is called to allow waiting threads of same priority to run. Any thread of higher priority than currently running thread will automatically preempt the currently running thread. I think option 1 is not completely right....
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
yield() means to move a thread to the 'ready' state. After that, it is up to the scheduler to decide what thread to run next.
The scheduler would decide based on priority. If there is a thread of higher priority than the yielded thread, then it will run that. Otherwise, it may run that same thread, or other thread of same priority.
So I think, (1) is the correct answer.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi
Answer 3 should be a valid answer. yield() method has no gauranteed behaviour. It can be implemented differently by different vendors so there is no gaurantee whether a higher, same or lower priority thread may be allowed.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Anupam Sinha:
..there is no gaurantee whether a higher, same or lower priority thread may be allowed.

But in general, the higher priority thread will be executed.
The 'Thread' api doc says that

Threads with higher priority are executed in preference to threads with lower priority.

I think what makes (1) correct is that it is more complete with its statement.
What happens if there are no threads with higher priority? Will it execute just any thread? No. If there are other threads with same priority then it may execute any one of them, including the yielded thread. Otherwise, the yielded thread will be executed again.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Alton
What happens if there are no threads with higher priority? Will it execute just any thread? No.

What you have said is to quite true to quite an extent but not the whole truth. It can if the thread scheduler wishes execute any thread of any priority totaly disregarding the priority of the currently executing thread. But that generally doesn't happens. In the question it says
Under what circumstances might you use the yield method of the Thread class
Now the word here is might so it means that you might want to do the following things. Whats not written is that there is no gaurantee that they will happen. Secondly as the number of correct answers are not mentioned both should be considered as correct. You might have find answer 1 more complete but answer 3 is not wrong and it never says select only one answer.
The scheduler would decide based on priority. If there is a thread of higher priority than the yielded thread, then it will run that.

Alton this is what exactly the option no. 3 says.
Actually there is no correct answer because as mentoned before a thread of lower priority may be allowed to run. But now that you have to select an answer I would go with 1 and 3.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with Thread question in Marcus Green