Docsity

Java Thread Priorities: Thread Scheduling and Context Switching, Lecture notes of Java Programming

Partial preview of the text

Download Java Thread Priorities: Thread Scheduling and Context Switching and more Lecture notes Java Programming in PDF only on Docsity!

Java Thread

Thread Priorities

  • (^) Java assigns to each thread a priority that determines how that thread should be treated with respect to the others.
  • (^) Thread priorities are integers that specify the relative priority of one thread to another.
  • (^) A thread’s priority is used to decide when to switch from one running thread to the next.
  • (^) This is called a context switch.

Thread Priorities

  1. A thread can voluntarily relinquish control.
    • (^) This is done by explicitly yielding,
    • (^) sleeping, or
    • (^) blocking on pending I/O.
    • (^) In this scenario, all other threads are examined, and the highest-priority thread that is ready to run is given the CPU.

Thread Priorities

  1. A thread can be preempted by a higher-priority thread.
    • (^) In this case, a lower-priority thread that does not yield the processor is simply preempted—no matter what it is doing— by a higher-priority thread.
    • (^) Basically, as soon as a higher-priority thread wants to run, it does.
    • (^) This is called preemptive multitasking

Thread Priorities

  • (^) To set a thread’s priority, use the setPriority( ) method, which is a member of Thread.
  • (^) This is its general form: final void setPriority(int level)
  • (^) Here, level specifies the new priority setting for the calling thread.
  • (^) The value of level must be within the range MIN_PRIORITY and MAX_PRIORITY.
  • (^) Currently, these values are 1 and 10 , respectively.
  • (^) To return a thread to default priority, specify NORM_PRIORITY , which is currently 5.
  • (^) These priorities are defined as static final variables within Thread.