Menu
Topics Index
...
`

Threads1577


If you need explanation Read this topic

If you need Answer Take test on this topic

Threads1577
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());
        }
    }
}

A. t1 Priority = 3, t2 Priority = 4, t3 Priority = 5, 
B. Thread-0 Priority = 4, Thread-1 Priority = 4, Thread-2 Priority = 4,
C. Thread-0 Priority = 4, Thread-1 Priority = 5, Thread-2 Priority = 6,
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: What is Multitasking and Multithreading?

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is C...as t1.run(),t1.run(),t3.run() methods are called in priorityDemo class constructor, so they will be executed as functions instead of seperate threads..so the output is sequential and the default names given to threads t1,t2,t3 are Thread-0,Thread-1,Thread-2

Posted by Krishna Sai    2013-07-03 14:21:09


c is the answer since 3 threads run in sequence and after the sleep duration is over the respective prio value is through getPrio method is printed

Posted by Karteek Paruchuri    2013-07-03 14:21:49


Ans is E.. becoz getname and getpriority are class methods so they are called using thread like thread.getName()....

Posted by Om Prakash Om    2013-07-03 14:27:28


ans is C bcoz we r directly running the threads for the first two cases.. after execution of constructor of priority the thread t1 runs..and only after the execution of thread t1 completes, the thread t2 is created and its run method is called...and only after that thread t3.start is called and it executes in som tym...the result may hav been different if we would have used .start in first or second or both cases..so now as threads executes in the same sequence as given... so thread 0 has priority 4 and thread 1has priority 5 and thread 2 has pririty 6 as constructors are executed in d same sequence...

Posted by Pramod Jain    2013-07-03 15:16:35


c

Posted by Ram Reddy Bitla    2013-07-04 04:15:58


Congratulations Krishna Sai Mulpuri . You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-04 08:24:50


Received rc!! thanks :)

Posted by Krishna Sai    2013-07-05 16:18:00


© meritcampus 2019

All Rights Reserved.

Open In App