Monday, December 9, 2013

Thread Priorities

  • Every thread has a priority.
  • The priority of a thread is used to inform the thread scheduler how important the thread to get picked.
  • Threads with higher priority are executed in preference to threads with lower priority.
  • Below are the thread priorities that Java API provides
    • MAX_PRIORITY - The maximum priority a thread can have.
    • NORM_PRIORITY - The default priority that is assigned to a thread.
    • MIN_PRIORITY - The minimum priority that a thread can have.
  • Java API offers method to get and set the priorities of the thread.
    • getPriority() method:
                               Return the thread's priority.
    • setPriority() method:
                              Changes the priority of this thread. First checkAccess() method of this thread, to determine if the currently running thread has permission to modify the thread, is called with no arguments. This may result in throwing SecurityException. If the priority is not in the range MIN_PRIORITY and MAX_PRIORITY, an IllegalArgumentException will be thrown.

    • toString() method:
                             Returns a string representation of the thread, including thread name, priority, and thread group.

  • The priority of a newly created thread is set equal to the priority of the thread creating it, that is, the currently running thread. The method setPriority() may be used to change the priority to a new value.

No comments:

Post a Comment