Overview of Threads

A sequence or flow of execution in a Java program is called a t_hread. _

Threads are also known as light weight processes, as they share the same data and process address space.

Thread Priorities

Every thread has a priority based on which it gets preference for execution. Threads can have priorities ranging from [1…10].

Below is a list of some of the constants defined in the Thread class for specifying the thread priority:

  • java.lang.Thread.MIN_PRIORITY = 1
  • java.lang.Thread.NORM_PRIORITY = 5
  • java.lang.Thread.MAX_PRIORITY = 10

Thread Scheduling

Threads are scheduled by Java runtime using fixed priority scheduling algorithm.

Threads with higher priority generally (but this is not guaranteed) execute before threads with lower priority.

However, JVM may decide to execute a lower priority thread before a higher priority thread to prevent starvation. A thread may, at any time, also decide to give up its right to execute by calling the Thread.yield() method.

Threads can only yield the CPU to other threads having the same priority but if a thread attempts to yield to a lower priority thread the request is ignored.



#java #programming #developer

Threads in Java
1.75 GEEK