Menu
Topics Index
...
`

Worker Name


If you need explanation Read this topic

If you need Answer Take test on this topic

Worker Name
What will be the output of the following program?
public class Worker extends Thread {
    Worker() {
        setName("Ashok");
        setPriority(Thread.NORM_PRIORITY);
    }
    public void run() {
        System.out.println("Name = " + Thread.currentThread().getName());
        System.out.println("Priority = " + Thread.currentThread().getPriority());
    }
    public static void main(String[] args) {
        Worker w = new Worker();
        w.run();
    }
}

A. Name = Ashok
Priority = 1
B. Name = Ashok
Priority = 5
C. Name = main
Priority = 5
D. Name = main
Priority = 1
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 b.w.run in main func invokes run() here 1sop prints thread name Ashok,bcz we set thread name using setName.thn norm priority value 5 get printed

Posted by Maheshwari Natarajan    2015-01-05 08:54:30


ANS IS C

In this program concept of "Threads" are used.

--Here class called Worker is created which extends Thread.Inside the constructor name of the thread is set by setName() method.Name of this thread is set as "Ashok".

--To set the priority setpriority() method is used.Priority is set as NORM_PRIORITY which is "5".

--Run() method is overriden in which Name of the currentThread is printed using getName() function.Here the thread running is "main" thread so "main" will get printed,Ashok will not get printed.To print "Ashok" getName() should be used without Thread.currentThread() as currentThread refers to main thread.
Priority is printed using getPriority() method which is 5.This priority is of "main" method not the one which is set inside constructor of worker.

--Inside main() method object of worker is created.run() method is called using this object.

Posted by Mânïshå Mùlchåndânï    2015-01-05 09:26:16


c

Posted by Shubham Bansal    2015-01-05 09:43:07


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Puneeth Shivalingaiah. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-06 05:18:24


© meritcampus 2019

All Rights Reserved.

Open In App