This document discusses Java Synchronization in multithreading and provides information about:
1.Synchronization in multithreading.
2.Introduction of Synchronization.
3.Why Synchronization is important.
4.Different ways of Synchronization.
5.Examples on Synchronization.
6.Different ways to achieve Synchronization
7.Synchronization real world application.
2. 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.
5. 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.
6. 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.
7. 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
8. 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.
13. 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
16. 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.
23. 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.
24. 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.
25. 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