Multithreading


  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. 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 the thread in output of this program?
    public class Output
    {
    public static void main(String args[])
    {
    Thread thread = Thread.currentThread();
    System.out.println(thread.isAlive());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘thread.isAlive’ returns true.


  1. What is the name of the thread in output of this program?
    public class multithreading_NameExample
    {
    public static void main(String args[])
    {
    Thread thread = Thread.currentThread();
    thread.setName("Thread One");
    System.out.println(thread.getName());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘Thread One’.



  1. What is the priority of the thread in output of this program?
    public class multithreaded_PriorityExample
    {
    public static void main(String args[])
    {
    Thread thread = Thread.currentThread();
    thread.setName("Thread One");
    System.out.println(thread.getPriority());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    The getPriority() function is used to obtain the priority of the thread, in this code the priority given to thread is ‘5’.