Menu
Question Index
...

What will be the output of the following program?

public class PriorityDemo {
    Priority t1, t2, t3;
    public PriorityDemo() {
        t1 = new Priority();
        t1.run();
        t2 = new Priority();
        t2.run();
        t3 = new Priority();
        t3.start();
    }
    public static void main(String args[]) {
        new PriorityDemo();
    }
}
class Priority extends Thread implements Runnable {
    int sleep;
    static int prio = 3;
    public Priority() {
        sleep += 100;
        prio++;
        setPriority(prio);
    }
    public void run() {
        try {
            Thread.sleep(sleep);
            System.out.print(getName() + " Priority = " + getPriority() + ", ");
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
}


t1 Priority = 3, t2 Priority = 4, t3 Priority = 5,
Thread-0 Priority = 4, Thread-1 Priority = 4, Thread-2 Priority = 4,
Thread-0 Priority = 4, Thread-1 Priority = 5, Thread-2 Priority = 6,
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