Open In App

Output of Java Programs | Set 46 (Multi-Threading)

Last Updated : 04 Oct, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report
Prerequisite : Multi-threading in Java 1. What will be the output of the program? JAVA
class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
    }
}
Options: 1. One thread created 2. Two thread created 3. Depend upon system 4. No thread created Output:
 The answer is option (2)
Explanation : In the above program, one thread will be created i.e. the main thread which is responsible to execute the main() method and the child thread will be created after the execution of t.start() which is responsible to execute run() method. 2. What will be the output of the program? JAVA
class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.run();
    }
}
Options: 1. One thread created 2. Two thread created 3. Depend upon system 4. No thread created Output:
 The answer is option (1)
Explanation : In the above program only one thread will be created i.e. the main thread which is responsible to execute the main() method only. The run() method is called by the object t like a normal method. 3. What will be the order of output of the program? JAVA
class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main");
    }
}
Options: 1. Main Run 2. Run Main 3. Depend upon Program 4. Depend upon JVM Output:
 The answer is option (4)
Explanation : In the above program, we cant predict the exact order of the output as it is decided by the Thread scheduler which is the part of JVM. 4. What will be the output of the program? JAVA
class Test implements Runnable {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main");
    }
}
Options: 1. Main Run 2. Run Main 3. Compile time error 4. Depend upon JVM Output:
 The answer is option (3)
Explanation : In the above program, we will get compile time error because start() method is present in the Thread class only and we are implementing Runnable interface. 5. What will be the output of the program? JAVA
class Test implements Runnable {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Thread t1 = new Thread();
        t1.start();
        System.out.println("Main");
    }
}
Options: 1. Run 2. Main 3. Compile time error 4. Run Main Output:
 The answer is option (2)
Explanation : In the above program, we are calling start() method of Thread class which is responsible to execute run() method of Thread class and Thread class run() method has empty implementation. That's why one child thread will be created but it will not execute Test class run() method.

Practice Tags :

Similar Reads