• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Priority Question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul !
I got clarified now.
Default priority is Thread.NORM_PRIORITY and not 5.
- Paddy
reply
    Bookmark Topic Watch Topic
  • New Topic