Menu
Topics Index
...
`

ThreadDemo2908


If you need explanation Read this topic

If you need Answer Take test on this topic

ThreadDemo2908
What will be the output of the following program?
public class ThreadQuestion {
    public static void main(String[] args) {
        Thread t = new Thread();
        System.out.println("Before Priority = " + t.getPriority());
        t.setPriority(5);
        System.out.println("After Priority = " + t.getPriority());
        t.setName("Thread Question");
        System.out.println(t.getName());
    }
}

A. Before Priority = 0
After Priority = 5
Thread Question
B. Before Priority = 5
After Priority = 5
Thread Question
C. Before Priority = 1
After Priority = 5
Thread Question
E. Compilation Error or Runtime Error
Topic: Thread Concept In Java

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 B...here we have the Threads Priority concepts which is used full when multiple threads are waiting for the same resource than system will provide the chance to the thread which has highest priority..priorities has significance when more than threads are waiting..if we have one thread  than priority has no significance...in java we have priorities from 0-10 min is 0 and max is 10 and avg is 5...by default every thread has priority as 5..by using setPriority() we can set priority to the threads...getPriority() will return the priority of thread...here initially every threads has 5 and next we set to the 5 and we are printing so it will print 5 5 next we set name as "Thread Question" by default name is Thread-0

Posted by Uday Kumar    2015-01-23 14:41:21


ans is B  here execution starts with main() here thread t is created .t.getPriority() method  returns the thread's priority.  Default priority of a thread is 5  it prints "before priority=5" . To set a thread's priority, use the setPriority( ) method here setpriority is 5 next it prints "after priority=5". setName() method changes the name of this thread to be equal to the argument name.so t.setName("Thread Question ") here name changes to thread question it prints "Thread Question".

Posted by Ramya Reddy Addulla    2015-01-23 14:54:11


ANS IS B

In this program concept of "Thread Priority" are used.

--Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.thread priority is a key element in deciding which thread gets scheduled, although the overall priority of a thread is a combination of the process's priority class, the thread's relative priority (values from the table above), plus any temporary "boost" given in specific circumstances (such as on returning from a wait on I/O).

--Here class called ThreadQuestion is created in which Thread object is created.Priority of thread is displayed by using setpriority() method.Name of the thread is set by setName() method.

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

Following static constants are defined with priority:

public static int MIN_PRIORITY
public static int NORM_PRIORITY
public static int MAX_PRIORITY

--Priority is set to 5 and again it is displayed.

--->Before priority -5
After priority -5

--Then Name of the Thread is set by using setName to "thread question" and displayed by using getName() method.

---> Thread Question

Posted by Mânïshå Mùlchåndânï    2015-01-23 15:13:31


ans is b

Posted by Gaurav Bansal    2015-01-23 19:57:12


This dose is now closed and the winners are Uday Kumar,  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 ?????????? ?????. 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-26 07:12:32


© meritcampus 2019

All Rights Reserved.

Open In App