Basic of Scheduler And Its Advantage And Disadvantages
Posted By : Ganesh Chandra Tiwari | 24-Sep-2018
Scheduling:
Scheduling is a process for distributing or assigning a job to a resource that completes the activity. The job can be any task that must be assigned to the hardware, that is to the processors. Programming makes multitasking possible with a single CPU. A scheduler aims to improve performance (the total amount of work per unit of time), minimize waiting time for tasks, minimize latency or response time, maximize equity. Depending on the requirements, the user can opt for a different programming algorithm, as they often conflict with each other, ie performance with respect to latency.
Scheduling disciplines
These are the algorithms that distribute activity to the resource asynchronously. Some of them are described below:
1) First, come to Fist Served
This implements FIFO (First In First Out) and is the simplest scheduling algorithm.
Append the activity in the same order in which it was received
The change of context is only at the completion of the process.
Performance is low because the long task has a CPU for a long time.
There is no Starvation every process has its chance.
Wait and the response time is great for an activity that is the last in the queue
2) Earliest Deadline First
It is a dynamic scheduling algorithm also known as the shortest time to go.
In addition, it processes the task with the priority queue that performs the task based on priority
Each time a task is completed, another task will run closer to the closest expiration or priority.
3) Shortest Remaining Time First
The processor takes the shorter work, i.e. organizes all tasks based on the execution time
If there is a time-consuming task, it is interrupted to perform a new short task
It is necessary to estimate the execution time in advance before the task is performed.
It has maximum performance.
Starvation is possible for a large task with the shortest task in the process.
4) Fixed priority pre-emptive scheduling
The operating system provides a fixed priority range for each process and the scheduler organizes the activity in the queue based on its priority. Activities with lower priority are interrupted by a task with a higher priority.
The lowest priority task can be starvation with a high number of high priority queue activities for CPU time.
5) Round-Robin scheduling
The scheduler assigns a fixed time for activities and crosses them.
If the process is completed within the time interval provided, it is terminated, otherwise, it is rescheduled.
Create overload with a few small numbers of activities
Moreover, it has a good average response time and starvation can never happen in it.
Often in applications, you need to perform some special background tasks to perform some jobs in a range. The example can be a service running in the background for cleaning the application. We have the collection of useless Java data called Java Garbage collection.
There are 3 different ways to achieve this goal
using simple thread
using TimerTask
using ScheduledExecutorService
Using Simple Thread
This is very simple, what creates the simple thread puts it to work forever with the use of the while loop and makes use of the sleep method to put the interval between runs.
This is simply a quick and quick way to reach it. The following is the code for this.
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
Using the timer and TimerTask
The previous method we saw was as fast as possible but lacks some features
This has many more advantages than the previous ones are as follows
check on when to start and cancel the activity
the first execution can be delayed if desired, it provides profits
In this we use the Timer class for the purpose of programming and TimerTask is used to enclose the activity that will be performed within its run () method.
The timer instance can be shared to schedule multi-tasking and is thread-safe.
When the timer constructor is called, it creates a thread and this single thread is used for any task schedule.
For our purpose, we use Timer # scheduleAtFixedRate
The following code shows the use of Timer and TimerTask
import java.util.Timer;
import java.util.TimerTask;
public class Task2 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
@Override
public void run() {
// task to run goes here
System.out.println("Hello !!!");
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
intevalPeriod);
} // end of main
}
These classes are existing classes from JDK 1.3.
Using ScheduledExecutorService
This is inserted into java.util.concurrent from Java SE 5 as a concurrency utility. This is the preferred way to achieve the goal.
Offers the following advantages over previous solutions
The pool of threads is used to execute as compared Timer`s single thread
Provides the flexibility to delay the first run
It provides beautiful conventions to provide time intervals
The following code shows the use of it,
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}
In this, we use ScheduledExecutorService # scheduleAtFixedRate as shown, take as executable as part of the code we want to execute, initial delay for the first run
Thanks, I hope it will help you
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ganesh Chandra Tiwari
Ganesh is a Web Application Developer. He has knowledge of Spring, Struts, Hibernate, Angular JS, HTML, CSS, Javascript.