Starvation Of Threads In Java

Swapnil Kant
Javarevisited
Published in
3 min readOct 18, 2020

Definition: Starvation of thread in java is said to occur when a particular thread does not get access to the object or the resource which leads to an increase in waiting and execution time.

Starvation is said to occur when two or more threads are allocated to the C.P.U. (Central Processing Unit) and takes a lot of time in execution, due to which other waiting threads cannot get the C.P.U. for its execution to carry on.

Causes Of Starvation:

There are many reasons for causes of starvation of threads in java, some of them are described below:

  • High Priority Running Thread: There may be a case where a high priority thread is running by occupying the C.P.U. and it needs heavy processing which requires a lot of time in completion, so for this work to get completely executed the other threads which have a low priority order have to wait for a long time which leads to starvation.
  • Synchronized Block In Java: There may be a case where the order in which the threads are allowed to enter the synchronized block is granted the resources in the same order as they are programmed to be scheduled, which results in waiting for the resources and the objects by another thread leading to starvation, where the other threads other than a particular thread are given the C.P.U. for its execution.
  • Threads Waiting On An Object Remains Waiting Forever: The notify() method in java has no track on the threads that which particular thread is wakening if there are multiple threads, therefore there may be a risk that any of the thread is being processed and the other threads are never called for execution.

Example Of Starvation In Threads

class Starvation extends Thread { 
static int count = 1;
public void run() {
System.out.println(count + " Thread execution starts");
System.out.println("Thread execution completes");
count++;
}
public static void main(String[] args) throws InterruptedException {
System.out.println("Parent thread execution starts");

/* Priority of each thread given. */
/* Thread 1 with priority 7. */
Starvation thread1 = new Starvation();
thread1.setPriority(7);
/* Thread 2 with priority 6. */
Starvation thread2 = new Starvation();
thread2.setPriority(6);
/* Thread 3 with priority 5. */
Starvation thread3 = new Starvation();
thread3.setPriority(5);
/* Thread 4 with priority 4. */
Starvation thread4 = new Starvation();
thread4.setPriority(4);
/* Thread 5 with priority 3. */
Starvation thread5 = new Starvation();
thread5.setPriority(3);

thread1.run();
thread2.run();
thread3.run();
thread4.run();

/* Here thread 5 have to wait because of the
other threads */
thread5.run();

System.out.println("Parent thread execution completes");
}
}

Output Of The Above Code Will be:

Parent thread execution starts
1 Thread execution starts
Thread execution completes
2 Thread execution starts
Thread execution completes
3 Thread execution starts
Thread execution completes
4 Thread execution starts
Thread execution completes
5 Thread execution starts
Thread execution completes
Parent thread execution completes.

Important Points To Remove Starvation:

Some of the important points to remove starvation of threads are given as follows:

  • By implementation of the Thread.yield() method, so that when the thread in the process after releasing the lock gets a fair chance to occupy the C.P.U. and can get some time to complete its execution till the original thread again gets the control over the C.P.U.
  • One can also use the Thread.sleep() method to given chance to other Threads for execution.

Other Thread Articles you may like

--

--

Swapnil Kant
Javarevisited

Hi, I am Swapnil Kant, an avid programmer, and a full-time learner! One who is highly interested in Algorithm Optimization and Development