0
\$\begingroup\$

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.
    while(!waitingMAp.isEmpty()){
        process.addAll(this.getRoundRobin(waitingMAp));
    }
    System.out.print(process);
    System.out.println();
    System.out.print(waitingTimeList);
}

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

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

        if(entry.getValue()>commonInt){
            waitingMAp.put(entry.getKey(),entry.getValue()-commonInt);
        }
        }

    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

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Naming

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

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.

Whitespace

Consider using it more liberally, especially between operators.

entry.getValue() > commonInt

is easier to read than

entry.getValue()>commonInt

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();
waitingTimeList.add(timepassed);

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.

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

Your Answer

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

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