Synchronization in Java Multithreading
Synchronization in Java Multithreading
Name: Date:
Antanios Kaissar 17-Nov-2022
Outline
Definitions Lock Concept Types of Conclusion
in Java Synchronizatio
n
2
Definition: Multithreading in Java
A thread is a lightweight subprocess, the smallest unit of processing.
Multithreading is the method of running two or more threads at the
same time to maximize CPU utilization. As a result, it is often referred
to as Concurrency in Java. Each thread runs in parallel with the
others.
Why?
Memory conservation: several threads do not assign different memory areas.
Why?
Synchronization helps in preventing thread interference.
4
Lock Concept in Java
Synchronization Mechanism is implemented by using the synchronized keyword
in java language. It is built on top of the locking mechanism.
This locking mechanism is taken care of by Java Virtual Machine (JVM).
The synchronized keyword is applicable for methods, blocks, classes, it can’t
be applied to variables. Synchronized keyword in java creates a block of code is
known as a critical section. To enter the critical section, thread needs to obtain
the corresponding object’s lock.
5
Deadlock in Java
Here, both threads are waiting for each other to unlock resources R1 &
R2.
But thread 2 can’t release lock for resource R2 until it gets hold of
6
Types of Synchronization
7
Mutual Exclusive: 1. Synchronized Method
If we use the Synchronized keyword in any method, then that method is
Synchronized Method.
It is used to lock an object for any shared resources.
The object gets the lock when the synchronized method is
called.
The lock won’t be released until the thread completes its
Syntax:
function.
Access_modifiers synchronized return_type method_name
(Method_Parameters)
Public synchronized void increment (int x) {
// Code of the Method.
8
Mutual Exclusive: 2. Synchronized block
Suppose you don’t want to synchronize the entire method; you want to
synchronize few lines of code in the method.
A synchronized block helps to synchronize those few lines of code. It will take the
object as a parameter. It will work the same as Synchronized Method.
In the case of synchronized method lock accessed is on the method but in the
case of synchronized block lock accessed is on the object.
Syntax:
synchronized (object) {
//code of the block.
}
9
Mutual Exclusive: 3. Static
Synchronization
In java, every object has a single lock (monitor) associated with it. The thread which is
entering into synchronized method or synchronized block will get that lock.
Suppose in the case of where we have more than one object, in this case, two separate
threads will acquire the locks and enter a synchronized block or synchronized method with a
separate lock for each object at the same time.
To avoid this, we use static synchronization. synchronized keyword is placed before the static
method. Here, lock access is on the class not on object and Method.
Syntax:
1. Wait () : It causes the current thread to place itself into the waiting stage until another
thread invokes the notify() method or notifyAll() method for this object.
2. Notify () : It wakes up a single thread called wait () on the same object. Awakened
thread will not be able to proceed until the current thread release lock.
3. NotifyAll () : This will wake up all the threads waiting on this object monitor.
11
Cooperation (Inter Thread Communication in
java)
13
THANK
YOU
14