0% found this document useful (0 votes)
3 views6 pages

Unit 4 Synchronization

Synchronization is a process that manages access to shared resources in multithreaded programs to prevent data corruption and ensure thread safety. In Java, synchronization is implemented using the 'synchronized' keyword, which allows only one thread to access a method or block of code at a time. There are two types of synchronization in Java: synchronized methods, which lock the entire method, and synchronized blocks, which lock only a specific section of code.

Uploaded by

Abirami Durai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Unit 4 Synchronization

Synchronization is a process that manages access to shared resources in multithreaded programs to prevent data corruption and ensure thread safety. In Java, synchronization is implemented using the 'synchronized' keyword, which allows only one thread to access a method or block of code at a time. There are two types of synchronization in Java: synchronized methods, which lock the entire method, and synchronized blocks, which lock only a specific section of code.

Uploaded by

Abirami Durai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 4

SYNCHRONIZATION
5. SYNCHRONIZATION:

Synchronization is a process that controls the access of multiple threads to shared resources. When
multiple threads try to access the same resource (like a variable, method, or object) at the same time,
it can lead to inconsistent or incorrect data. Synchronization ensures that only one thread can
access the resource at a time, preventing data corruption and ensuring thread safety.

Why is Synchronization Needed?


• In multithreaded programs, threads often share common data or resources.
• Without synchronization, two or more threads might try to update the same data simultaneously.
• This causes race conditions, where the output depends on the sequence of thread execution,
leading to unpredictable results.
How Synchronization Works in Java?

• Java provides the synchronized keyword to control access to a block of code or a method. When a

method or block is declared synchronized:

• The thread acquires a lock (monitor) on the object before executing the code.

• Other threads must wait until the lock is released.

• This ensures mutual exclusion — only one thread can run the synchronized code at a time.
Types of Synchronization in Java

Synchronized Method

• The entire method is locked so only one thread can run it at a time.
• Declared by adding synchronized keyword to the method.
Example:
public synchronized void display() {
// code here
}
Synchronized Block
• Only a part (block) of the method is locked, not the whole method.
• Allows better control and improves performance by locking only necessary code.
Example:
public void display() {
synchronized(this) {
// critical code here
}
}
Thank You

You might also like