I tried to implement round robin in java please check and help me to improve it Thanks:

package trial.java;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Roundrobin1 {
//common interval for all process.
static int commonInt=3;
//to store a list of queued process
Map<String, Integer> waitingMAp=new LinkedHashMap<>();
List<Integer>waitingTimeList=new ArrayList<>();
//to Store elapsed time.
int timepassed=0;

public  void mainL(){
    //input process name and time in order.
    waitingMAp.put("p0", 3);
    waitingMAp.put("p1", 3);
    waitingMAp.put("p2", 3);
    waitingMAp.put("p3", 9);
    //first time getting the list of process.
    List<String>process=new ArrayList<>();
    //it will call recursively till no process is waiting.

    public List<String> getRoundRobin(Map<String, Integer> 
    Map<String, Integer> Local=new LinkedHashMap<>();
    List <String>curr=new ArrayList<>();
    for(Map.Entry<String, Integer> entry :Local.entrySet()){

   waitingTimeList.add(entry.getValue()>3?timepassed=timepassed+3:     (timepassed=timepassed+entry.getValue()));


    return curr;

    public static void main(String[] a){
    new Roundrobin1().mainL();



it will return below result: first row is order and second row is time elapsed. [p0, p1, p2, p3, p3, p3] [3, 6, 9, 12, 15, 18]

I would like to add couple of questions here

  1. Do I need to take priorities of the process as well in the input.If yes how it going to solve starvation problem.

  2. How to scale up this algorithm for multiprocessor or multi-core systems


1 Answer 1



Your variables, class and methods aren't very well named.

The class Roundrobin1 should be Pascal case. It's also discouraged from using numbers in class names. If you are thinking of doing implementations of Round Robin, perhaps DefaultRoundRobin would be okay. If not, just RoundRobin is fine.

You have a local variable named Local. Variables should always be camel case to distinguish them from classes.

You also have a few inconsistencies such as timepassed, which should be timePassed, and waitingMAp which should be waitingMap.

It's not clear what the method mainL is supposed to do. Give it a more specific name.


Fields should be declared with the lowest access possible. Your fields can be private as they are not required outside of the class. Fields should be declared as final if you do not expect them to change.


Consider using it more liberally, especially between operators.

entry.getValue() > commonInt

is easier to read than


Compound Statements

Specifically this line:

waitingTimeList.add(entry.getValue()>3?timepassed=timepassed+3:     (timepassed=timepassed+entry.getValue()));

has way too much going on. You could refactor it to:

timepassed += (entry.getValue() > 3) ? 3 : entry.getValue();

which is much more readable.

Magic Numbers

You have a few seemingly random 3s and 9s. Consider using constants (static final fields) with appropriate names.

  • \$\begingroup\$ thanks for the comments ..i was in a hurry then i will apply these changes. \$\endgroup\$
    – Adward
    Apr 5, 2017 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.