Java Programming - Threads - Discussion

15. 

What will be the output of the program?

public class ThreadTest extends Thread 
{ 
    public void run() 
    { 
        System.out.println("In run"); 
        yield(); 
        System.out.println("Leaving run"); 
    } 
    public static void main(String []argv) 
    { 
        (new ThreadTest()).start(); 
    } 
}

[A]. The code fails to compile in the main() method
[B]. The code fails to compile in the run() method
[C]. Only the text "In run" will be displayed
[D]. The text "In run" followed by "Leaving run" will be displayed

Answer: Option D

Explanation:

No answer description available for this question.

Manish said: (Jul 17, 2010)  
Yield is a static method. It makes the currently running thread head back to the runnable state to allow other threads of same priority to get their turn, but there is no such guarantee.

Sudhansu said: (Dec 20, 2010)  
yield() is a static method . Thes how we are invoking it for an instance using this.

Smith said: (Oct 9, 2011)  
What is output for this program ?

Like
"In run
Leaving run"
or
like
"In run Leaving run"

Venky said: (Dec 9, 2012)  
Is this possible to call super class's static method in sub class's non-static method without specifying classname(ie.yeild()).

Shanki said: (Aug 16, 2013)  
yield() allow other Threads to execute by stopping the current Thread. Since there are no such other Threads present, therefore yield() not blocks the current Thread and execution continues after yield().

Vikas Grover said: (Mar 12, 2014)  
I think it will give an compile time error, as we yield is static method, hence how can we call a static method in non static method. If we correct this like Thread.yield () ; then it will print in run leaving run, because yield method stop current thread and allow other thread to invoke, hence there is no other thread and this will continue after calling yield.

Anonymous said: (Dec 19, 2014)  
Output will be :

In run.
Leaving run.

Its fine for compilation, just because calling yield() inside run().

Srinivas said: (Jan 30, 2015)  
Yield method causes to pause the current executing thread and give the chance for remaining waiting thread of the same priority. If there are no waiting threads or all waiting threads are having low priority then the same thread will continue it's execution.

Output will be :

In run.
Leaving run.

Vinu said: (Oct 6, 2015)  
Yes @Srinivas you are correct and also it is depending on "thread scheduler" since there is no other thread so same thread will continue the process. I hope this if I am wrong please tell.

Kratika said: (Jul 3, 2016)  
Yield method only delays the current thread for some time or can it shift the priority.

Meghna said: (May 28, 2017)  
yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should.

Otherwise, the current thread will continue to run.

Sujata said: (Feb 8, 2018)  
Output is:
In run
Leaving run

Because, yeild() method only delays the current thread and gives the chance to same priority thread. In this case, there is no any thread present, so after some delay in will continue the execution.

Rksh25 said: (Nov 20, 2019)  
It works the same whether you call it with an instance or Thread.yield(). The reason is there no code to display in the main method after start() a child thread. write some SOPs in main after start a child thread. CPU gives the chance to the main thread. Since in this program there a 2 threads main, Thread-0.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.