Menu
Question Index
...

What will be the output of the following program?

class NThread implements Runnable {
    String str;
    Thread t;
    NThread(String nameofthread) {
        str = nameofthread;
        t = new Thread(this, str);
        t.run();
    }
    public void run() {
        try {
            for (int i = 5; i > 3; i--) {
                System.out.print(str + " : " + i + ", ");
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {    System.out.println(str + "Interrupted");    }
        System.out.println(str + " exiting");
    }
}
class MultiThreadDemo {
    public static void main(String[] args) {
        new NThread("FIRST");
        new NThread("SECOND");
        new NThread("THIRD");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {    System.out.println("Main Thread Interrupted");    }
        System.out.println("Main Thread Exiting");
    }
}


FIRST : 5, SECOND : 5, THIRD : 5, SECOND : 4, FIRST : 4, THIRD : 4,
Main Thread Exiting
SECOND exiting
FIRST exiting
THIRD exiting
FIRST : 5, SECOND : 5, THIRD : 5, Main Thread Exiting
SECOND : 4, FIRST : 4, THIRD : 4, SECOND exiting
THIRD exiting
FIRST exiting
FIRST : 5, FIRST : 4, FIRST exiting
SECOND : 5, SECOND : 4, SECOND exiting
THIRD : 5, THIRD : 4, THIRD exiting
Main Thread Exiting
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App