Question

I know that Threads can have Priority ranging from MIN_PRIORITY(1) to MAX_PRIORITY(10). However, if I have more than 10 threads to execute, how will I assign priority to them ? Can the priority be more than 10 ?

Was it helpful?

Solution

However, if I have more than 10 threads to execute, how will I assign [a different] priority to [each of] them ?

You can't.

But it should not matter. The chances are that thread priorities will only give you a rough prioritization anyway. Certainly, the javadocs do not give any strong guarantees about how (or even if) thread priorities affect thread scheduling.

The actual implementation of thread scheduling and thread priorities on a modern JVM is done by the operating system's thread scheduler. Java Thread priorities are really little more than a "hint" to the operating system.

The bottom line is that an Java application that relies on thread priorities for correct behaviour is likely to be unreliable, and to behave differently / incorrectly if run on a different platform to the one you originally developed / debugged for. You should never rely on priorities for correct behaviour. Use the synchronization primitives provided to ensure that things occur in the required order.


If you need precise behaviour of thread priorities, you will need to use a "real-time Java" JVM running on top of a "real-time operating system".

OTHER TIPS

The javadoc states the following about setPriority(int)

Throws:

IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.

Depending on the implementation, you'll need to specify an appropriate value. You won't be able to have a finer priority setting with Thread.

no u can't assign priority which is above 10 since u wil be having a max priority is 10 and min priority is 1 ..even though if u assign with priority above 10 it can be treated as a default priority and the program starts to work according to that priority.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top