×

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another name for it. Scheduling in FCFS is quite similar to this. The round robin task scheduling and its use in a Java programme will be covered in this section.

Round Robin Algorithm

The preemptive process scheduling algorithm is called round robin scheduling. Every process is given a certain time to run cyclically. The term "fixed time" is also used for the concept of "time quantum," "time slot," and "time stamp." Once a process has run for a predetermined amount of time, it is preempted and another process runs for the remainder of that time. State-saving for preempted processes is accomplished by context switching. The algorithm's goal is to use the CPU as much as possible. The time sharing scheme is where the algorithm works best.

The Round Robin algorithm comes in the following three variations:

  • Deficit Round Robin
  • Selfish Round Robin
  • Skip Round Robin

We shall first comprehend the different temporal factors associated with the Round Robin algorithm's procedure before going on to the example.

Arrival Time: This refers to the instant that a process transitions into the ready state. It denotes that the procedure is prepared for use. It is determined by:

Response time is determined by subtracting the arrival time from the moment the process first receives the CPU.

Response Time: This is the period of time during which a process initially receives the CPU.

Turnaround Time: This is the length of time a procedure is carried out within the system. These formulae are used to compute it:

Turnaround Time = Completion Time - Arrival Time 

Waiting Time: This is the length of time required for a process to finish running. In other words, the amount of time a process spends waiting for the CPU while it is ready. It is calculated using the formula below:

            Waiting Time = Turnaround Time - Burst Time

Burst Time: The amount of time a process spends running on the CPU in total. Another name for it is execution time.

Completion Time: This is the length of time it takes for a procedure to be finished. It's also referred to as "exit time."

Throughput time: The quantity of tasks the CPU completes in a specific length of time is known as throughput time. It is employed to determine a CPU's efficiency.

 Benefits of Round Robin Scheduling

  • It has a cyclical structure.
  • Never does it result in famine.
  • The CPU performs each task for a set amount of time.
  • not giving any work a priority.
  • The schedule is similar to FCFS.

Round Robin Scheduling Java Program

public class Main
{
    // How to calculate the total waiting time?
    static void findWaitingTime(int processes[], int n,
                 int bt[], int wt[], int quantum)
    {
        // Create a duplicate of "burst times bt" to save any leftovers.
        // burst times.
        int rem_bt[] = new int[n];
        for (int i = 0 ; i < n ; i++)
            rem_bt[i] =  bt[i];
       
        int t = 0; // Current time
       
        // Keep going round-robin with the procedures.
        // until none of them have finished.
        while(true)
        {
            boolean done = true;
       
            //repeatedly go through each procedure one at a time
            for (int i = 0 ; i < n; i++)
            {
                // Whenever a process's burst time exceeds 0
                // then, just more processing is required.
                if (rem_bt[i] > 0)
                {
                    done = false; // There is a pending process
       
                    if (rem_bt[i] > quantum)
                    {
                        // The displays' value should be raised
                        // how long a procedure has been running
                        t += quantum;
       
                        // Reduce the current process's burst time.
                        // by quantum
                        rem_bt[i] -= quantum;
                    }
       
                    //if burst time falls within or matches
                    // Quantum. This process' final cycle
                    else
                    {
                        // The displays' value should be raised
                        // how long a procedure has been running
                        t = t + rem_bt[i];
       
                        // Current time less waiting time is the waiting time.
                        // used by this process
                        wt[i] = t - bt[i];
       
                        // As the procedure is completed
                        // set the time left for its blast to be 0
                        rem_bt[i] = 0;
                    }
                }
            }
       
            // if all operations are completed
            if (done == true)
              break;
        }
    }
       
    // Technique for estimating turnaround time
    static void findTurnAroundTime(int processes[], int n,
                            int bt[], int wt[], int tat[])
    {
        // By adding, you can determine turnaround time.
        // bt[i] + wt[i]
        for (int i = 0; i < n ; i++)
            tat[i] = bt[i] + wt[i];
    }
       
    // How to estimate the average time
    static void findavgTime(int processes[], int n, int bt[],
                                         int quantum)
    {
        int wt[] = new int[n], tat[] = new int[n];
        int total_wt = 0, total_tat = 0;
       
        //function to determine how long each process will delay
        findWaitingTime(processes, n, bt, wt, quantum);
       
        //Calculate the turnaround time for each procedure.
        findTurnAroundTime(processes, n, bt, wt, tat);
       
        // Display procedures and any relevant information.
        System.out.println("Processes " + " Burst time +
                      " Waiting time " + " Turnaround time");
       
        // Calculate total waiting time and total turn
        // around time
        for (int i=0; i<n; i++)
        {
            total_wt = total_wt + wt[i];
            total_tat = total_tat + tat[i];
            System.out.println(" " + (i+1) + "\t\t" + bt[i] +"\t " +
                              wt[i] +"\t\t " + tat[i]);
        }
       
        System.out.println("Average waiting time = " +
                          (float)total_wt / (float)n);
        System.out.println("Average turnaround time = " +
                           (float)total_tat / (float)n);
    }
      
    // Driver Method
    public static void main(String[] args)
    {
        // process id's
        int processes[] = { 1, 2, 3};
        int n = processes.length;
       
        // Burst time of all processes
        int burst_time[] = {6,7, 8};
       
        // Time quantum
        int quantum = 2;
        findavgTime(processes, n, burst_time, quantum);
    }
}

Output:

Processes Burst time Waiting time Turnaround time
 1          6            12               18
 2          7            20               27
 3          8            13              21
Average waiting time = 15.0
Average turnaround time = 22

Related Topics

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.