0

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Aslam a
  • 750
  • 10
  • 19

3 Answers3

1

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.

Peter Walser
  • 15,208
  • 4
  • 51
  • 78
0

You can enumerate all threads in the current program via ThreadGroup, see for example this answer.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
0

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.

JimmyB
  • 12,101
  • 2
  • 28
  • 44
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24