0% found this document useful (0 votes)
53 views

Round Robin Java

The document implements a priority scheduling algorithm in Java. It defines a Process class with attributes like ID, arrival time, burst time and priority. It then adds sample processes to a list, sorts them by priority in a queue and simulates their execution, tracking waiting time and turnaround time.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Round Robin Java

The document implements a priority scheduling algorithm in Java. It defines a Process class with attributes like ID, arrival time, burst time and priority. It then adds sample processes to a list, sorts them by priority in a queue and simulates their execution, tracking waiting time and turnaround time.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;

class Process {

int id;

int arrivalTime;

int burstTime;

int priority;

public Process(int id, int arrivalTime, int burstTime, int priority) {

this.id = id;

this.arrivalTime = arrivalTime;

this.burstTime = burstTime;

this.priority = priority;

public class PriorityScheduling {

public static void main(String[] args) {

List<Process> processes = new ArrayList<>();

processes.add(new Process(1, 0, 3, 3));

processes.add(new Process(2, 1, 5, 2));

processes.add(new Process(3, 2, 2, 1));

processes.add(new Process(4, 3, 6, 4));

priorityScheduling(processes);

public static void priorityScheduling(List<Process> processes) {

int currentTime = 0;
int totalWaitingTime = 0;

int totalTurnaroundTime = 0;

PriorityQueue<Process> queue = new PriorityQueue<>(Comparator.comparingInt(p -> p.priority));

while (!processes.isEmpty() || !queue.isEmpty()) {

if (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {

queue.add(processes.remove(0));

continue;

if (queue.isEmpty()) {

currentTime = processes.get(0).arrivalTime;

continue;

Process currentProcess = queue.poll();

totalWaitingTime += currentTime - currentProcess.arrivalTime;

currentTime += currentProcess.burstTime;

totalTurnaroundTime += currentTime - currentProcess.arrivalTime;

System.out.println("Process " + currentProcess.id + " completed at time " + currentTime);

System.out.println("Average waiting time: " + (double) totalWaitingTime / (double) queue.size());

System.out.println("Average turnaround time: " + (double) totalTurnaroundTime / (double)


queue.size());

You might also like