Explain thread priority and what is default thread priority?

Explain thread priority and what is default thread priority?

Explain thread priority and what is default thread priority?

Explain thread priority and what is default thread priority?
  • Every thread in java has some priority. It may be default priority generated by JVM or customized priority provided by the programmer.
  • The valid range of thread priorities is 1-10. where 1 is min priority and 10 is the max priority.
  • Thread class defines the following constant to represent some standard priorities.
    • Thread.MIN_PRIORITY — >1
    • Thread.MAX_PRIORITY –>10
    • Thread.NORM_PRIORITY –>5
  • Thread scheduler will use priorities while allocating processer.
  • The thread which is having highest priorities will get chance first.
  • If two threads having same priorities then we can’t expect exact execution order. It depends on thread scheduler.
  • Logically we can say that threads run simultaneously but practically its not true, only one Thread can run at a time in such a way that user feels that concurrent environment.
  • Fixed priority scheduling algorithm is used to select one thread for execution based on priority.
  • Thread class defines the following methods to get and set a priority of a thread
    • setPriority(10);
    • getPriority();

Default Priority

  • The default priorities only for the main thread is 5. but for all remaining thread, default priority will be inherited from parent to child that is whatever priority parent thread has the same priority will be there for the child thread.
  • Some platform won’t provide proper support for thread priorities.

public static void main(String[] args) {
System.out.println(Thread.currentThread().getPriority()); -->5
Testing t = new Testing();
System.out.println(t.getPriority()); -->5
t.setPriority(7);
System.out.println(t.getPriority()); -->7
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*