No video

06. Difference between Volatile & Synchronized - Java

  Рет қаралды 6,764

WebEncyclop Tutorials

WebEncyclop Tutorials

Жыл бұрын

#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 Intervi...
Next Video:
• 07. How to create Sing...
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:
/ @webencyclop
Java Interview Questions Playlist:
• Java Developer Intervi...

Пікірлер: 21
@urszulastolarek2977
@urszulastolarek2977 21 күн бұрын
You explain things very well!!! Thank you! 🙂
@N.T.R564
@N.T.R564 Жыл бұрын
Hi, your explanation along with diagram is like spoon feeding. Please make more vedios on multithreading and concurrency, waiting for your vedios 😊❤
@webencyclop
@webencyclop Жыл бұрын
sure thanks 👍
@kajaraja100
@kajaraja100 19 күн бұрын
you explain very well. Please make more videos on core java and spring courses. Please added more spring boot, hibernate and java interview questions.
@youssef8107
@youssef8107 Ай бұрын
Excellent explanation
@chaituy2672
@chaituy2672 Жыл бұрын
Good to see you back again
@webencyclop
@webencyclop Жыл бұрын
thank you, please let us know if you got us any feedback.
@Nilcha-2
@Nilcha-2 2 ай бұрын
Simple and excellent explanation. Simple and excellent graphics. 👍
@premChowdaryNim
@premChowdaryNim 10 ай бұрын
I really like the way you teach. Could you please make more videos on java 8 and above concepts. I never forgot the concepts if I learn like this.
@SaveAndGrowwww
@SaveAndGrowwww Жыл бұрын
Good to see you again after long time😊
@webencyclop
@webencyclop Жыл бұрын
yeah, thanks 🙏. Many more coming ahead. if you got any suggestions please let us know.
@parambharti7095
@parambharti7095 Жыл бұрын
Very nice way teaching. Please keep uploading videos like this.
@webencyclop
@webencyclop Жыл бұрын
thank you. yes, uploading more every week. please let us know if you have any topic on your mind for video
@sanjanachauhan1256
@sanjanachauhan1256 18 күн бұрын
Please start stream and lambda coding and theory questions. Loved you explanations Thank you so much for all the knowledge u are sharing,why don't you upload frequently?
@kajaraja100
@kajaraja100 19 күн бұрын
how to handle cache machanism in core java -> this is my interview question. Please explain
@Akash-hx9mn
@Akash-hx9mn Жыл бұрын
this is really helpful, keep up the good work. thank you.
@nenuanenenuane6645
@nenuanenenuane6645 10 ай бұрын
Great vedio please continue vedio series on Springboot if possible make vedio on spring security Thanks
@handsome6959
@handsome6959 5 ай бұрын
You are best
@user-vh7mf2uv4o
@user-vh7mf2uv4o Жыл бұрын
Long time no see ?
@webencyclop
@webencyclop Жыл бұрын
yeah, I'll try keep posting new videos more frequently now. If you got any suggestions, please let us know.
@user-vh7mf2uv4o
@user-vh7mf2uv4o Жыл бұрын
@@webencyclop yep please make some videos on new java features like Lambdas, functional interface etc
07. How to create Singleton Class - Java Interview
7:40
WebEncyclop Tutorials
Рет қаралды 10 М.
Harley Quinn lost the Joker forever!!!#Harley Quinn #joker
00:19
Harley Quinn with the Joker
Рет қаралды 22 МЛН
My Cheetos🍕PIZZA #cooking #shorts
00:43
BANKII
Рет қаралды 22 МЛН
01. Internal Working of HashMap & Java-8 Enhancement
19:11
WebEncyclop Tutorials
Рет қаралды 109 М.
Java Concurrecy: Volatile vs Atomic - Java Programming
10:50
Will Tollefson
Рет қаралды 2,3 М.
02. Equals and HashCode Contract & Different Variations
11:19
WebEncyclop Tutorials
Рет қаралды 55 М.
Лекция 11. Многопоточность в Java
1:42:36
Computer Science Center
Рет қаралды 36 М.
Harley Quinn lost the Joker forever!!!#Harley Quinn #joker
00:19
Harley Quinn with the Joker
Рет қаралды 22 МЛН