aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Priority Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Priority Question" Watch "Thread Priority Question" New topic
Author

Thread Priority Question

sam pitt
Ranch Hand

Joined: Mar 07, 2001
Posts: 48
In KAM (page 284 in my book), at one point the author mentions that " If no explicit thread priority is specified for a thread, it is given the default priority of 5"
After this, right in the next sentence he says
"A thread inherit the priority of its parent thread"
What is right ?
-Sam
Alex Sbityakov
Ranch Hand

Joined: Jul 23, 2001
Posts: 49
The two statements are not contradictory. If the thread has a parent class which sets the priority of the class, then it inherits that priority. If it doesn't have a parent class (in other words, if is a direct subclass of Thread) then it inherits the default priority of Thread, which is 5.
The two statements really reflect a single behavior of the class.
Correct me if I'm wrong,
Alex
Padmaja Balaji
Ranch Hand

Joined: Jun 13, 2001
Posts: 122
Hey folks!
Some bullet points to remember reg. priority of threads
* The priority is an integer from 1 to 10 being MAX_PRIORITY 10, MIN_PRIORITY 1 and NORM_PRIORITY 5.
* Default priority is 5
* You can set the thread's priority using setPriority() method by passing in the desired new priority
* A thread inherit the priority of its parent thread if there is one.
* (Refer RHE book page 207) The Java specification states that threads must have priorities but it does not dictate precisely what the scheduler should do about priorities. Algorithms that rely on manipulating thread priorities might not run consistently on all platforms.
Have I covered in one stroke. Anything else !!!
Paddy
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
To say "If no explicit thread priority is specified for a thread, it is given the default priority of 5" is wrong.
1. The correct statement should be "If no explicit thread priority is specified for a thread, it is given the priority of it's parent."
2. Although NORM_PRIORITY is 5, the API does not say that. So it is advised not to use 5 as such. Always use Thread.NORM_PRIORITY. To say "the default priority is 5" is therefore wrong. The correct statement is "the default priority is Thread.NORM_PRIORITY"

Every thread except the "main" thread has a parent.
HTH,
Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
Forgot to mention:
>in other words, if is a direct subclass of Thread
Here, parent does not mean superclass or subclass. Parent means the thread that started this thread. That's why every thread except the main thread has a parent.
Understand the difference between "a thread" and "java.lang.Thread".
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi Paul ,
After reading your first post i tried the source & i was amazed to find these declarations for the Thread fields
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
Correct me . I maybe wrong(new to the source thing) .
Peace
Ashish
[This message has been edited by Ashish Hareet (edited July 30, 2001).]
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Just like to make another addition ,
t.setPriority(11);//Thread t = Thread.currentThread()
compiles fine but gives an IllegalArgumentException at run-time (source again) .
In fact something like
t.setPriority(-1);
will also compile .
Peace
Ashish
Padmaja Balaji
Ranch Hand

Joined: Jun 13, 2001
Posts: 122
Thanks Paul !
I got clarified now.
Default priority is Thread.NORM_PRIORITY and not 5.
- Paddy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Priority Question