0% found this document useful (0 votes)
8 views

Synchronization in Java Multithreading

The document provides an overview of multithreading and synchronization in Java, explaining that multithreading allows multiple threads to run concurrently to optimize CPU usage. It discusses the importance of synchronization for controlling access to shared resources, the lock concept, and various types of synchronization methods. Additionally, it addresses potential issues such as deadlocks and inter-thread communication mechanisms.

Uploaded by

Ela Ray
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Synchronization in Java Multithreading

The document provides an overview of multithreading and synchronization in Java, explaining that multithreading allows multiple threads to run concurrently to optimize CPU usage. It discusses the importance of synchronization for controlling access to shared resources, the lock concept, and various types of synchronization methods. Additionally, it addresses potential issues such as deadlocks and inter-thread communication mechanisms.

Uploaded by

Ela Ray
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

College of Engineering

Department of Computer Science &


Engineering
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.

Time: Switching between threads takes less time.

Enhances program structure by making it simpler and easier to navigate


3
Definition: Synchronization in java
 Synchronization in java is the capability to control the access of
multiple threads to any shared resource. The synchronization is
necessary for reliable communication between threads.

Why?
Synchronization helps in preventing thread interference.

Synchronization helps to prevent concurrency problems.

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

The process is nothing but a


Process
Thread
program under execution. It runs Synchronizati
Synchronization
on
independently isolated from
another process. The resources Cooperation (Inter
Mutual Thread
like memory and CPU time, etc. Exclusive Communication in
java)
are allocated to the process by
the operation System.
Synchronized Synchronized Static
Method block 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:

synchronized static return_type method_name (Parameters) {


//code
}
10
Cooperation (Inter Thread Communication in
java)
 There is a situation on the thread that keeps on checking some conditions repeatedly,
once that condition satisfies thread moves with the appropriate action. This situation is
known as polling.
 This is a wastage of CPU time, to reduce the wastage of CPU time due to polling, java
uses Inter – Thread Communication Mechanism using the following three methods
defined in object class:

wait () notify() notifyAll()

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)

1. Threads enter to acquire lock.


2. Lock is acquired by on thread.
3. Now thread goes to waiting state if you call wait() method on the object. Otherwise, it releases
the lock and exits.
4. If you call notify() or notifyAll() method, thread moves to the notified state (runnable state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the lock and exits the monitor state of the object.12
Conclusion

We have discussed the following:


 Multithreading in java is the method of running two or more threads
in parallel.
 Synchronization and is necessary when more than one thread want to
access shared date.
 Lock concept in java as well as deadlock.
 Synchronization types and the definition of each type.

13
THANK
YOU
14

You might also like