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?
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....
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.
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.
Joined: May 30, 2003
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.
Joined: Apr 13, 2003
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.
subject: Issue with Thread question in Marcus Green