Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 15)
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(); 
    } 
}
The code fails to compile in the main() method
The code fails to compile in the run() method
Only the text "In run" will be displayed
The text "In run" followed by "Leaving run" will be displayed
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 2 of 2.

Meghna said:   8 years ago
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:   7 years ago
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:   5 years ago
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.
(1)


Post your comments here:

Your comments will be displayed after verification.