Multiple choice

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(); 
    } 
}

  1. The code fails to compile in the main() method

  2. The code fails to compile in the run() method

  3. Only the text "In run" will be displayed

  4. The text "In run" followed by "Leaving run" will be displayed

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The Thread.yield() method suggests to the thread scheduler that the current thread is willing to yield its current use of processor, but the scheduler is free to ignore this suggestion. In this simple single-threaded program, yield() has no visible effect, and both statements execute normally, printing 'In run' followed by 'Leaving run'.