Java Program for Shortest Job First (SJF) Scheduling [Preemptive & Non-Preemptive]

Here you will get java program for shortest job first (sjf) scheduling algorithm, both preemptive and non-preemptive.

Shortest job first scheduling algorithm can also be known as shortest job next scheduling. It is very easy to implement and efficient in reducing average response time. Now we will see how it will work with the example and its implementation.

In shortest job first, we should know the execution time of each process before running. This we can estimate in many ways. This is the prerequisite for SJF.

Also Read: Java Program for First Come First Serve (FCFS) Scheduling Algorithm

Suppose we have set of processes are in ready queue. The SJF scheduling algorithm will choose the job which has shortest remaining time to complete. We have 2 variations of this SJF algorithm that are preemptive and non-preemptive. Preemptive version of SJF also known as SRTF.



Process id

Arrival time

Burst time

P1 0 3
P2 0 1
P3 0 2

We have 3 processes in our ready queue. As we discussed SJF will schedule the job which is having least execution time or burst time.

Gantt Chart

sjf 1

Now we will calculate the completion time, waiting time, turnaround time of each process.

Process id Completion time Waiting time Turnaround time
P1 6 3 6
P2 1 0 1
P3 3 1 3

Average waiting time = (3+0+1)/3 = 1.33

Turnaround time = (6+1+3)/3 = 3.33

Java Program for Shortest Job First (SJF) Scheduling (Non-Preemptive)


sjf 2


  • It will result minimum average waiting time to each process.


  • It will create starvation problem to long process.
  • Execution time should be estimated prior to the scheduling. It will incur some cost.

Now we will see preemptive version of SJF that is SRTF (shortest remaining tie first)


In SRTF the selection of job is same like in SJF. But the difference is In SJF process will run till completion. In SRTF process will run till completion or a new process added into queue which is having smaller execution time than the current process remaining execution time.

Process id Arrival time  Burst time
P1 2 3
P2 1 2
P3 3 4
P4 5 6

When process is added to queue or process is completed then only CPU may switch the process.

Gantt Chart

sjf 3

Process id Completion time Waiting time Turnaround time
P1 6 1 4
P2 3 0 2
P3 10 3 7
P4 16 5 11

Average turnaround time = (4+2+7+11)/4 = 6.0

Average waiting time = (1+0+3+5)/4 = 2.25

Java Program for Shortest Job First (SRTF) Scheduling (Preemptive)


sjf 4


  • Shorter jobs will be scheduled quickly.
  • Less average waiting time.


  • Starvation for longer jobs.

Comment below if you have doubts related to above program for sjf in Java.

9 thoughts on “Java Program for Shortest Job First (SJF) Scheduling [Preemptive & Non-Preemptive]”

  1. Bro plz provide round robin algorithm in Java
    Your way of programming is awesome
    Easier to understand thanks alot plz provide
    Round robin algorithm in Java find it in 2 to3 hr
    If u find it thane send me as mail

  2. What is the variable ‘min’ for?

    Why is “min=999” only?

    Can ‘min’ be of any different value? If not, why?

Leave a Comment

Your email address will not be published. Required fields are marked *