SYNCHRONIZATION IN MULTITHREADING
WHAT IS SYNCHRONIZATION SYNCHRONIZATION IN JAVA IS THE CAPABILITY TO CONTROL THE ACCESS OF MULTIPLE THREADS TO ANY SHARED RESOURCE. IN MULTITHREAD ENVIRONMENT IT ENSURES THAT THE SHARED RESOURCE CAN BE ACCESSED BY ONLY ONE THREAD AT A TIME.
WHY SYNCHRONIZATION 01 Avoid Race Condition 03 Thread Safety 02 Mutual Exclusion 04 Ordering 05 Consistency 06 Efficiency
Types of Synchronization There are two types of synchronization 1.Process Synchronization 2.Thread Synchronization
1.PROCESS SYNCHRONIZATION Process synchronization refers to the coordination and control of multiple processes in a multitasking or multi-processing environment. In a multitasking operating system, multiple processes run concurrently, and they may need to access shared resources or communicate with each other.
2.THREAD SYNCHRONIZATION Thread synchronization in Java is a technique used to control access to shared resources by multiple threads to prevent race conditions and ensure data consistency. It ensures that only one thread can access a critical section of code or a shared resource at a time.
MUTUAL EXCLUSIVE Mutual Exclusive helps keep threads from interfering with one another while sharing data. It can be achieved by using the following three ways: By Using Synchronized Method 1. 2.By Using Synchronized Block 3.By Using Static Synchronization
1.BY USING SYNCHRONIZED METHOD synchronized methods is a way to control concurrent access to methods or blocks of code in Java. When a method is declared as synchronized, only one thread can execute that method at a time for a given instance of the class.
WITHOUT SYNCHRONIZED METHOD
OUTPUT
SYNCHRONIZED METHOD
OUTPUT
In Java, a synchronized block is a block of code that is marked as synchronized, allowing only one thread to execute the block at a time. This is useful when multiple threads need to access shared resources or critical sections of code concurrently, and you want to ensure that only one thread can access the critical section at any given time to prevent race conditions and maintain data consistency. 2.By Using Synchronized Block
SYNCHRONIZED BLOCK
OUTPUT
3.BY USING STATIC SYNCHRONIZED Static synchronization, also known as class-level synchronization, refers to the synchronization of static methods or code blocks in Java. When a method or code block is declared as synchronized and static, it means that the synchronization is applied at the class level rather than at the instance level. This ensures that only one thread can execute the synchronized static method or code block across all instances of the class at any given time.
WITHOUT STATIC SYNCHRONIZATION
WITH STATIC SYNCHRONIZATION
OUTPUT
WITHOUT STATIC SYNCHRONIZATION
OUTPUT
Synchronization Class Level Lock SUNCHRONIZATION WITH LOCKS Object Level Lock
Class Level Lock For the shared resource, each object, only one object can have access at a time, other objects of the shared resource will wait.
Object Level Lock An object level lock, on the other hand, involves synchronizing access to individual objects rather than entire classes. Each object instance has its own lock, and threads must acquire the lock associated with a specific object before accessing synchronized methods or code blocks of that object.
APPLICATIONS 01 Operating System 03 Security and Encryption 02 Distributed Systems 04 Manufacturing and Robotics 05 Concurrency Control in Web Servers 06 Supply Chain Management
Java Presentation On Synchronization in Multithreading

Java Presentation On Synchronization in Multithreading

  • 1.
  • 2.
    WHAT IS SYNCHRONIZATION SYNCHRONIZATION INJAVA IS THE CAPABILITY TO CONTROL THE ACCESS OF MULTIPLE THREADS TO ANY SHARED RESOURCE. IN MULTITHREAD ENVIRONMENT IT ENSURES THAT THE SHARED RESOURCE CAN BE ACCESSED BY ONLY ONE THREAD AT A TIME.
  • 3.
    WHY SYNCHRONIZATION 01 AvoidRace Condition 03 Thread Safety 02 Mutual Exclusion 04 Ordering 05 Consistency 06 Efficiency
  • 4.
    Types of Synchronization Thereare two types of synchronization 1.Process Synchronization 2.Thread Synchronization
  • 5.
    1.PROCESS SYNCHRONIZATION Process synchronizationrefers to the coordination and control of multiple processes in a multitasking or multi-processing environment. In a multitasking operating system, multiple processes run concurrently, and they may need to access shared resources or communicate with each other.
  • 6.
    2.THREAD SYNCHRONIZATION Thread synchronization inJava is a technique used to control access to shared resources by multiple threads to prevent race conditions and ensure data consistency. It ensures that only one thread can access a critical section of code or a shared resource at a time.
  • 7.
    MUTUAL EXCLUSIVE Mutual Exclusivehelps keep threads from interfering with one another while sharing data. It can be achieved by using the following three ways: By Using Synchronized Method 1. 2.By Using Synchronized Block 3.By Using Static Synchronization
  • 8.
    1.BY USING SYNCHRONIZEDMETHOD synchronized methods is a way to control concurrent access to methods or blocks of code in Java. When a method is declared as synchronized, only one thread can execute that method at a time for a given instance of the class.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    In Java, asynchronized block is a block of code that is marked as synchronized, allowing only one thread to execute the block at a time. This is useful when multiple threads need to access shared resources or critical sections of code concurrently, and you want to ensure that only one thread can access the critical section at any given time to prevent race conditions and maintain data consistency. 2.By Using Synchronized Block
  • 14.
  • 15.
  • 16.
    3.BY USING STATICSYNCHRONIZED Static synchronization, also known as class-level synchronization, refers to the synchronization of static methods or code blocks in Java. When a method or code block is declared as synchronized and static, it means that the synchronization is applied at the class level rather than at the instance level. This ensures that only one thread can execute the synchronized static method or code block across all instances of the class at any given time.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    Class Level Lock Forthe shared resource, each object, only one object can have access at a time, other objects of the shared resource will wait.
  • 24.
    Object Level Lock Anobject level lock, on the other hand, involves synchronizing access to individual objects rather than entire classes. Each object instance has its own lock, and threads must acquire the lock associated with a specific object before accessing synchronized methods or code blocks of that object.
  • 25.
    APPLICATIONS 01 Operating System 03Security and Encryption 02 Distributed Systems 04 Manufacturing and Robotics 05 Concurrency Control in Web Servers 06 Supply Chain Management