Difficulty: Easy
Correct Answer: The text "In run" followed by "Leaving run" will be displayed
Explanation:
Introduction / Context:
This question checks understanding of Thread.yield() and the typical behavior when a single custom thread is launched. yield() is a hint to the scheduler that the current thread is willing to let other runnable threads proceed.
Given Data / Assumptions:
Concept / Approach:
yield() does not terminate a thread; it merely gives the scheduler an opportunity to allow another runnable thread to run. In the absence of competing runnable threads with higher or equal priority, the yielding thread is typically scheduled again immediately. Thus both print statements execute in order.
Step-by-Step Solution:
Verification / Alternative check:
Even with other runnable threads, yield() does not skip code; the same thread will continue later and print the second line.
Why Other Options Are Wrong:
Common Pitfalls:
Overestimating the power of yield(); it is only a scheduling hint and guarantees nothing beyond allowing other runnable threads a chance.
Final Answer:
The text "In run" followed by "Leaving run" will be displayed
Discussion & Comments