06. Difference between Volatile & Synchronized - Java

Published: 30 April 2023
on channel: Ankit Wasankar
7,731
213

#java #interviewquestions #webencyclop

In this playlist I will try to cover all the important java interview questions along with answers in very intuitive, kind of pictorial/animated way.

Complete Playlist:
   • Java Developer Interview Questions  

Next Video:
   • 07. How to create Singleton Class - J...  


In this series we will be covering all Java interview questions and answers. This is going to be comprehensive list of Java interview questions which can help experienced Java developers to prepare for an interview as well as upskilling the Java knowledge.

I would always recommend everyone to watch Durga Sir (durgasoft) videos because of extensive in depth explaination on all topics if you have more time.
But here we will try to cover important aspects of Java Programming which are always asked in interviews withing less time.



What is the difference between Volatile and Synchronized keyword Java.

Here in this video, we are going to go deeper at CPU cores and RAM level to understand how volatile and synchronized keywords will be used.

We will look into 2 major problems that we face in multi-threading and how they are handled using these "volatile" and "synchronized" keyword.

And we will also see what will happen if we don't use those keywords in multi-threaded environment in Java programming.

Volatile keyword is used only infront of instance variables and for volatile instance variables, processor will always fetch latest value from RAM also known as main memory. Volatile keyword will restrict caching of values.



In Java, the `volatile` and `synchronized` keywords are used for different purposes related to concurrency and multithreading.

The `volatile` keyword is used to indicate that a variable's value may be modified by multiple threads simultaneously. When a variable is declared as `volatile`, the Java memory model ensures that all threads see the latest value of the variable, and any write to the variable by one thread becomes visible to all other threads immediately.

However, `volatile` does not provide any synchronization or locking mechanism. It only ensures visibility of changes made by one thread to all other threads. Therefore, `volatile` is often used for flags, counters, and other variables that are read and written by multiple threads but do not require any locking.

On the other hand, the `synchronized` keyword is used to enforce mutual exclusion and ensure that only one thread can execute a critical section of code at a time. When a method or block is declared as `synchronized`, a thread must acquire a lock on the object before entering the critical section. If another thread attempts to enter the same critical section while the lock is held by another thread, it must wait until the lock is released.

`synchronized` provides both visibility and synchronization mechanisms and is useful when multiple threads access and modify shared data structures. However, `synchronized` can be expensive in terms of performance because it involves acquiring and releasing locks, which can cause contention and delays.

In summary, `volatile` is used to ensure visibility of changes made to a variable by multiple threads, while `synchronized` is used to enforce mutual exclusion and ensure that only one thread can access a critical section of code at a time.

Here is a summary of the key differences between `volatile` and `synchronized` in Java:

`volatile`:
Indicates that a variable's value may be modified by multiple threads simultaneously
Ensures visibility of changes made to the variable by one thread to all other threads immediately
Does not provide any synchronization or locking mechanism
Useful for flags, counters, and other variables that are read and written by multiple threads but do not require locking

`synchronized`:
Enforces mutual exclusion and ensures that only one thread can execute a critical section of code at a time
Provides both visibility and synchronization mechanisms
Involves acquiring and releasing locks, which can cause contention and delays
Useful when multiple threads access and modify shared data structures


Keywords:
Java, volatile, synchronized, volatile keyword, synchronized keyword, multi-threading, multithreading, CPU, Processor, RAM, Main Memory, Processor Cache, Restrict Caching, Race Condition, Critical Code Block, Sequential execution, parallel execution

#java
#interviewquestions
#volatile&synchronized
#multithreading
#cpu
#processor_caching
#critical_code
#race_condition
#deadlock
#parallel
#sequential


Like and subscribe:
   / @ankit.wasankar  

Java Interview Questions Playlist:
   • Java Developer Interview Questions  


Watch video 06. Difference between Volatile & Synchronized - Java online without registration, duration hours minute second in high quality. This video was added by user Ankit Wasankar 30 April 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 7,73 once and liked it 21 people.