Easy learning with example program codes

Starvation in java


Starvation is a situation when a thread is in waiting state from long period because it not getting access of shared resources or because higher priority threads are coming.


 * This program is used to show the starvation problem. 
 * @author codesjava
public class StarvationExample implements Runnable{
	private final Object resource;
	private final String message;
	private final boolean fair;
	public static void main(String[] args)
		boolean fair = false;
		if (args != null && args.length >= 1 && args[0].equals("fair")) {
			fair = true;
		// get the number of available CPUs, do twice as much threads.
		final int cpus = Runtime.getRuntime().availableProcessors();
		System.out.println("" + cpus + " available CPUs found");
		final int runners = cpus * 2;
		System.out.println("starting " + runners + " runners");
		final Object resource = new Object();
		// create sample runners and start them
		for (int i = 1; i <= runners; i++) {
			(new Thread(new StarvationExample(resource, String.valueOf(i), fair))).start();
		// suspend main thread
		synchronized (StarvationExample.class) {
			try {
			} catch (InterruptedException ignored) {
	public StarvationExample(Object resource, String message, boolean fair)
		this.resource = resource;
		this.message = message;
		this.fair = fair;
	public void run()
		synchronized (this) {
			for (;;) {
				synchronized (resource) {
					try {
						(fair ? resource : this).wait(100);
					} catch (InterruptedException ignored) {
	private static void print(String str)
		synchronized (System.out) {


4 available CPUs found
starting 8 runners

Download this example.
Next Topic: Inter-thread communication in java with example.
Previous Topic: Deadlock in java with example.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki