Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Thread Scheduler in Java

The thread scheduler in Java is a vital component responsible for determining which threads should be executed and which ones should wait. It selects threads that are in the "runnable" state, meaning they are ready to be executed. When multiple threads are in the runnable state, the thread scheduler makes the decision of choosing one thread to execute while the others are temporarily ignored. To make this decision, the scheduler considers two main factors: priority and arrival time.



Priority: Each thread is assigned a priority value ranging from 1 to 10. A thread with a higher priority has a greater chance of being selected by the thread scheduler for execution.


Time of Arrival: When multiple threads have the same priority and are in the runnable state, the thread scheduler takes their arrival time into account. The thread that arrived first is given priority over the others and gets selected for execution.


By considering these factors, the thread scheduler efficiently manages the execution of threads in a Java program, ensuring that the most important and timely threads are given priority.





Thread Scheduler Algorithms

  • First Come First Serve Scheduling
  • Time-slicing scheduling
  • Preemptive-Priority Scheduling


First Come First Serve Scheduling


In the First Come First Serve scheduling algorithm for multithreading in Java, the scheduler chooses threads based on their arrival order in the runnable queue. The thread that arrives first will be selected first for execution.


Threads Time of Arrival
threads1 0
threads2 2
threads3 1
threads4 3
threads5 4

Thread threads1 arrived first, followed by Thread threads3, Thread threads2, Thread threads4, and Thread threads5, and the order in which the threads will be processed is determined by the time of arrival of the threads.






Preemptive Priority Scheduling


Priority for Prevention CPU Scheduling Algorithm is a pre-emptive CPU scheduling algorithm that operates based on a process's priority. The scheduler in this algorithm schedules tasks based on priority, which means that a higher priority process should be executed first.


Priority for Prevention CPU Scheduling Algorithm defines a rank for each process using a rank-based system, where lower rank processes have higher priority and higher rank processes have lower priority. For example, if this Preemptive Algorithm is to be used to execute ten processes, the process with rank 1 will have the highest priority, the process with rank 2 will have a comparatively lower priority, and the process with rank 10 will have the lowest priority.


Time-slicing scheduling


A time slice is a period of time during which a process is assigned to run in a preemptive multitasking CPU. Every time-slice, the scheduler runs each process. The length of each time slice can be very important in balancing CPU performance and responsiveness.





If the time slice is very short, the scheduler will require more processing time. If the time slice is too long, the scheduler will require more processing time.


When a process is assigned to the CPU, the clock timer for that time slice is set. If the process completes its burst before the time slice, the CPU simply swaps it out as in a traditional FCFS(first Come first Serve) calculation. If the time slice is the first to expire, the CPU moves it to the end of the ongoing queue.



Advantages of Time-slicing scheduling

  • Fair Allocation of CPU Resources: Time-slicing scheduling ensures that CPU resources are distributed equally among different processes or threads. This helps prevent any single process from dominating the CPU and allows for a balanced execution of tasks.
  • Equal Weight to All Processes: Each process or thread is given an equal opportunity to execute, regardless of its priority or importance. This promotes fairness and prevents any particular process from being favored over others.
  • Simple Integration: Time-slicing scheduling is straightforward to incorporate into a system. It doesn't require complex algorithms or extensive modifications, making it easier to implement and maintain.
  • Optimal Average Processing Time: Context switching, which is the mechanism used to save and switch between preempted processes, is efficiently utilized in time-slicing scheduling. This results in a good average processing time performance for the overall system.

Disadvantages of Time-slicing scheduling




  • Delayed Processor Output with Short Slicing Time: When the time quantum (slicing time) is set to a short duration, it can cause delays in the processor's output. This happens because frequent context switches add overhead and reduce the time available for actual processing.
  • Time Wasted on Context Switching: Context switching, the process of saving and restoring the state of preempted processes, requires time and resources. With time-slicing scheduling, there can be a significant amount of time wasted on these context switches, affecting overall system efficiency.
  • Impact of Time Quantum on Performance: The performance of time-slicing scheduling is heavily influenced by the chosen time quantum. Selecting an inappropriate time quantum can result in either poor responsiveness or excessive context switching, leading to reduced efficiency.
  • b Inflexible Process Priorities: Time-slicing scheduling does not allow for fixed process priorities. As a result, more important or critical tasks cannot be given higher priority over less important ones. This can be a limitation in scenarios where certain processes require immediate attention.
  • Difficulty in Determining Suitable Time Quantum: Finding the optimal time quantum for time-slicing scheduling can be challenging. It requires considering the characteristics of the system, workload, and desired performance, making it a complex task to determine the most suitable value.
Sleeping a thread in java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.