Home » JAVA Programming » Multithreading » Question
  1. What is the output of this program?
    class Mythread extends Thread 
    {
    Thread thread;
    Mythread()
    {
    thread = new Thread(this,"New Thread");
    thread.start();
    }
    public void run()
    {
    try
    {
    thread.join();
    System.out.println(thread.getName());
    }
    catch(Exception e)
    {
    System.out.print("Exception");
    }
    }
    }

    public class multithreaded_programing
    {
    public static void main(String args[])
    {
    new Mythread();
    }
    }
    1. New Thread
    2. Thread
    3. My Thread
    4. Compilation Error
    5. Runtime Error
Correct Option: E

join() method of Thread class waits for thread being called to finish or terminate, but here we have no condition which can terminate the thread, hence code ‘thread.join()’ leads to runtime error and nothing will be printed on the screen.



Your comments will be displayed only after manual approval.