Multithreading


  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. View Hint View Answer Discuss in Forum

    NA

    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.


  1. Which of these method of Thread class is used to find out the priority given to a thread?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    getPriority()



  1. What is the output of this program?
    class N extends Thread
    {
    Thread thread1,thread2;
    N()
    {
    thread1 = new Thread(this,"Thread");
    thread2 = new Thread(this,"Thread");
    thread1.start();
    thread2.start();
    }
    public void run()
    {
    thread2.setPriority(Thread.MAX_PRIORITY);
    System.out.println(thread2.equals(thread1));
    }
    }

    public class multithreaded_programing
    {
    public static void main(String args[])
    {
    new N();
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.


  1. What is the output of this program?
    class n extends Thread
    {
    Thread thread;
    n()
    {
    thread = new Thread(this,"New Thread");
    thread.start();
    }
    public void run()
    {
    System.out.println(thread.isAlive());
    }
    }

    public class multithreading_Example
    {
    public static void main(String args[])
    {
    new n();
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    isAlive() method is used to check whether the thread being called is running or not, here thread is the main() method which is running till the program is terminated hence it returns true.



  1. Which of these method of Thread class is used to Suspend a thread for a period of time?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    sleep()