Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread  RSS feed

 
Lancy Mendonca
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the default priority for a newly created thread. Is it a constant? How can I change the default priority for a thread?
Thankyou in advance
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default priority of any thread is the same as the priority of the parent thread. ie., every thread "inherits" the priority from its parent.
You can change the priority of any thread using the setPriority() method. It is interesting to note, the priority of any thread cannot be greater than the maximum priority of the ThreadGroup it belongs to.
Hope this helps,
Ajith
 
Lancy Mendonca
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am asking this question because. If I check the priority of the System thread and any thread created for the System group the value is 5. Is this the default value.
Also when I create a Thread in a Thread Group having Max priority let us say 3. If any thread is created within this group would its default priority be 3 or would it be 5.
I am asking this question as Bruce Eckel mentions that all threads are created with a default priority of 5 and only when we try to change the priority of the thread via setPriority does he narrow down to the maximum priority of the Thread Group.
Hence I am confused?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your observations are right.
Thread class defines three constants NORM_PRIORITY, MAX_PRIORITY and MIN_PRIORITY. The default priority is NORM_PRIORITY, which is the average of MIN and MAX priority. Usually their values are 1,5 and 10 respectively.
The thread gets its default priority by the parent thread. You can specifiy the value of maximum priority for a thread group by using the setMaxPriority() method. The value you pass in is the limit of priority value any threads in the thread group can have. However, the API documentation says "Threads in the thread group that already have a higher priority are not affected."
In your example, if a ThreadGroup has a max priority of 3, assuming you set the max priority before creating any threds( note the text in bold above ), the only thing you can conclude is NO thread belonging to the group can have a priority greater than 3.
To summarize, default priority comes from the parent thread. The maximum configurable priority of any thread is limited by the max priority of the thread group the thread belongs to.
Hope this clears your confusion
Ajith
[This message has been edited by Ajith Kallambella (edited August 16, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!