We know that if we execute the getcurrentthread.priority
method we will get the thread priority as 5. I am not able to get the answer for the threads having priority higher than the Main method.
-
4Please edit your question with more of what you want. It's not clear – RaceBase Aug 28 '13 at 07:47
-
Actually it's quite clear what he asked. You just have to read it twice :) – Joetjah Aug 28 '13 at 07:47
-
On a side note, I fail to imagine what a reasonable use case for this would be. Possibly, you are heading in the wrong direction. – JimmyB Aug 28 '13 at 08:00
3 Answers
I assume you mean that this is somehow the index of the thread in a priority queue, and that therefore at least 4 other threads must exist.
Well, this isn't the case, it's not an index but a value used to compare its priority with other concurrent threads, not only in your VM but also on your system. In fact, threads can have the same priority.
Side note: when setting the priorities on Threads, always use the constants MIN_PRIORITY
, NORM_PRIORITY
and MAX_PRIORITY
. If you need intermediate values, calcuate them using the constants:
int mediumHighPriority = (Thread.NORM_PRIORITY+Thread.MAX_PRIORITY)/2;
The constant values may get changed in the future (could have a wider range, or even be reversed so that lower number equals higher priority, or the NORM_PRIORITY could get lower or higher), if you use the constants rather than their value you're on the save side and the code gets more legible.

- 15,208
- 4
- 51
- 78
-
Thanks Peter. can u elaborate on this. at least 4 other threads must exist. – Aslam a Aug 28 '13 at 08:06
You can enumerate all threads in the current program via ThreadGroup, see for example this answer.
When a java application is started, the JVM creates the main ThreadGroup as a member of the system ThreadGroup and set the priority to default(i.e NORM_PRIORITY).
This only applies to the threads in the current Java program though and will not provide any information about all the other threads running in other processes in the OS.

- 12,101
- 2
- 28
- 44

- 8,317
- 1
- 18
- 24