Round Robin Example in Java Program

Round Robin Example in Java

In this example, We will show you simple program about, How to write Round Robin Example in Java. The example program were tested and shared in the same post.

Interface (RoundRobin.java)

package com.dineshkrish;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public interface RoundRobin<T> {

	public T select() throws RoundRobinException;

	public void add(T ref);

	public void remove(T ref);

	public void update(T ref);
}

Implementation (RoundRobinImpl.java)

package com.dineshkrish;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class RoundRobinImpl<T> implements RoundRobin<T> {

	public final List<T> myResources;
	public int myCount = 0;

	public RoundRobinImpl() {
		myResources = new ArrayList<>();
	}

	public RoundRobinImpl(List<T> initialNodes) {
		myResources = new ArrayList<>(initialNodes);
	}

	@Override
	public synchronized T select() throws RoundRobinException {
		if (myResources.isEmpty()) {
			throw new RoundRobinException("There are no resource in the container");
		}

		int divisor = myResources.size();
		int index = ((myCount++ % divisor + divisor) % divisor);

		return myResources.get(index);
	}

	@Override
	public synchronized void add(T ref) {
		if (!myResources.contains(ref)) {
			myResources.add(ref);
		} else {
			System.out.println("The resource already exist!!!");
		}
	}

	@Override
	public synchronized void remove(T ref) {
		if (!myResources.remove(ref)) {
			System.out.println("There are no resource to remove!!!");
		}
	}

	@Override
	public synchronized void update(T ref) {
		int index = myResources.indexOf(ref);
		if (index != -1) {
			myResources.set(index, ref);
		} else {
			System.out.println("There are no resource to update!!!");
		}
	}

	public boolean isEmpty() {
		return myResources.isEmpty();
	}

	public boolean contains(T ref) {
		return myResources.contains(ref);
	}
}

Exception (RoundRobinException.java)

package com.dineshkrish;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class RoundRobinException extends Exception {

	public RoundRobinException(String message) {
		super(message);
	}
}

Testing (RoundRobinTest.java)

package com.dineshkrish;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class RoundRobinTest {

	public static List<String> getResources() {

		List<String> resourceList = new ArrayList<String>();

		resourceList.add("instance_1");
		resourceList.add("instance_2");
		resourceList.add("instance_3");

		return resourceList;
	}

	public static void main(String[] args) {

		// creating container with resources
		RoundRobin<String> rr = new RoundRobinImpl<String>(getResources());

		try {

			// selecting the resource
			System.out.println(rr.select());
			System.out.println(rr.select());
			System.out.println(rr.select());

			System.out.println(rr.select());
			System.out.println(rr.select());
			System.out.println(rr.select());

		} catch (RoundRobinException e) {

			System.out.println(e.getMessage());
		}

	}
}

Download Source Code

You can download the source code from here

Output

instance_1
instance_2
instance_3
instance_1
instance_2
instance_3

References

1. Round Robin Algorithm

Hello everyone, I am a Founder of dineshkrish.com.