Java Memory Model in 10 minutes

  Рет қаралды 261,449

Defog Tech

Defog Tech

Күн бұрын

Пікірлер: 205
@70ME3E
@70ME3E 4 жыл бұрын
I'm going through this playlist to learn Java concurrency, and I have to say your explanation and presentation quality are amazing, and the diagrams are really helpful. thank you!
@DefogTech
@DefogTech 4 жыл бұрын
Wow, thanks!
@mohammedrahamathulla4040
@mohammedrahamathulla4040 3 жыл бұрын
@@DefogTech Why no videos from you😔?? All your subscribers are waiting
@bhavyashah2630
@bhavyashah2630 6 жыл бұрын
Awesome video...Please don't stop making such excellent videos.
@DefogTech
@DefogTech 6 жыл бұрын
Thank you for the motivation sir!
@gurupadamgundala5142
@gurupadamgundala5142 6 жыл бұрын
Please make more videos
@ITech2005
@ITech2005 5 жыл бұрын
Defog Tech More videos :)
@saravanprathi6956
@saravanprathi6956 3 жыл бұрын
More videos pleaseeeee
@NaturalPro100
@NaturalPro100 5 жыл бұрын
The best video i could find on youtube.
@iknowyourerightbut6246
@iknowyourerightbut6246 5 жыл бұрын
future me: volatile has semantics for memory visibility. Basically, the value of a volatile field becomes visible to all readers (other threads in particular) after a write operation completes on it. Without volatile, readers could see some non-updated value.
@Machinerium
@Machinerium 5 жыл бұрын
I had an interview question like that.. This is a very clean explanation , keep doing it!
@rohitsharma-xt8qe
@rohitsharma-xt8qe 5 жыл бұрын
I have never seen such an excellent explanation of JMM. Thanks a lot
@mrchief3383
@mrchief3383 4 жыл бұрын
Man, you are awesome. I was struggling trying to understand atomic operations and the use cases of volatile variables. Thank you so much
@AkshayNalavade18
@AkshayNalavade18 3 жыл бұрын
Volatile won't work here. You have to use Atomic integer.... You have explained it in another of your videos. Volatile vs Atomic
@vamsiKrishna96
@vamsiKrishna96 3 жыл бұрын
Hey, can you tell about yourself?
@monishchhadwa777
@monishchhadwa777 7 ай бұрын
I would absolutely recommend all Java engineers-whether junior or very senior-to watch this!
@Dedeepya9
@Dedeepya9 5 жыл бұрын
Started watching thread pool videos Nd then addicted to all of ur videos Nd subscribed..the way of explanation is precise , innovative Nd interesting.your wrk deserves more than a thnks..
@DefogTech
@DefogTech 5 жыл бұрын
Thank you for the kind words!! I am so happy its helping everyone
@sepraveen
@sepraveen 4 жыл бұрын
Yes are techie by heart
@shellindebted5328
@shellindebted5328 6 жыл бұрын
I tried multiple times myself to understand JMM, But this is the first time i got to learn something about them. Excellent Presentation. Hats of to you, You define perfection!!
@DefogTech
@DefogTech 6 жыл бұрын
I am far from perfect. Though, thanks much for the kind words sir!
@yifei663
@yifei663 6 жыл бұрын
May be you should read java language specification : docs.oracle.com/javase/specs/jls/se8/html/jls-17.html .
@StyleTrick
@StyleTrick 4 жыл бұрын
Hey great videos as always! I just wanted to ask, since primitives are passed by value and Objects are passed as reference, does the JVM only cache primitives into a threads local cache? If the value being updated was an Integer Object, e.g. new Integer(1), it would not need flushing as the object is passed by REFERENCE?
@suhani091088
@suhani091088 4 жыл бұрын
Always read these topic therotically, but could never visualise it like this you have explained..Great presentation man!
@coreyhartman4510
@coreyhartman4510 4 жыл бұрын
This video is excellent.
@viveksingh9120
@viveksingh9120 5 жыл бұрын
Awesome mate. You are really doing great, very impressive
@shaikshasayyad131
@shaikshasayyad131 5 жыл бұрын
Crystal clear explanation!!! thanks a million .. I observed that the code was working fine though i had not used the volatile keyword at 9:37. what could it be the reason ?
@DefogTech
@DefogTech 5 жыл бұрын
It will work 99% of the time, but java memory model doesn't guarantee it will work all the time, thus we need the keyword
@shaikshasayyad131
@shaikshasayyad131 5 жыл бұрын
Thanks a lot for your your response.
@VikiSangani
@VikiSangani 5 жыл бұрын
Very nicely explained. Please keep making such videos.👍👍
@niftymiller6057
@niftymiller6057 6 жыл бұрын
Very good and crisp way of explanation. Keep doing this good :) work
@DefogTech
@DefogTech 6 жыл бұрын
Thank you!
@vikrantharne8576
@vikrantharne8576 5 жыл бұрын
But in multithreaded environment u can not be sure about which thread will get chance though we can override thread schedules behavior by setting priority...so in this case we need synchronization? My another questions is when thread executes the run method is it the only method thread executes then what about about synchronized blocks and synchronized methods...do we need to write those inside run method.... please clear my doubts..
@DefogTech
@DefogTech 5 жыл бұрын
Its difficult to ensure one thread is scheduled before another even in case of priority (eg: if CPU is multi-core, both low and high priority threads can be scheduled together). So its better to code using concurrency utilities (eg: Latches, Semaphores, volatiles etc.) Yes, most of the code that we want to run in a separate thread (as opposed to main thread) needs to be in run or call methods of Runnable/Called. Though, in Java 8, CompletableFuture or other frameworks/libraries could take your code in Supplier/Function/or any form, and run it within runnable without us knowing about it.
@vikrantharne8576
@vikrantharne8576 5 жыл бұрын
@@DefogTech thanks for the reply
@studio2038
@studio2038 4 жыл бұрын
very simple to understand
@gurupadamgundala5142
@gurupadamgundala5142 6 жыл бұрын
Awesome. Expected more videsos 👌👌👌
@DefogTech
@DefogTech 6 жыл бұрын
Thank you sir!
@contactdi8426
@contactdi8426 2 жыл бұрын
I am really amazed sir by your knowledge. Can you please share how you plan and learn all these things/concepts? Do you require them all these in your office related work regularly? I have been in IT industry for around 4-5 years but literally never faced these things, therefore never felt the need either to learn these. Please share some suggestions as senior developers to juniors on learning paths.
@vikaskokae
@vikaskokae 2 жыл бұрын
Exactly my feelings after been in the industry for a couple of decades. Learning from experience when applying concepts to real life work enhances understanding of concepts to the next level, something that prevail for years. I would be interested to know from Deepak, how he went about approaching concepts.
@tanoysaha7410
@tanoysaha7410 5 жыл бұрын
Your explanations are precise, but have so in-depth/hidden informations and moreover so easy to understand
@ganeshpathak4804
@ganeshpathak4804 5 жыл бұрын
Hi, I have one request if you could make a video on Performance improvement techniques. Not necessarily related to threading but in general. I am sure everyone would be benefited as you explain complex things in very simple way :)
@DefogTech
@DefogTech 5 жыл бұрын
Sure, sounds like a good idea. Will add it to the list
@saravanprathi6956
@saravanprathi6956 3 жыл бұрын
You are a fantastic teacher.. please please make more videos
@masteradityamishra9486
@masteradityamishra9486 6 жыл бұрын
Very nice explanation appreciate it, thanks.
@purnimadas3762
@purnimadas3762 5 жыл бұрын
That was really good. Thanks for posting, excellent, clear and to the point presentation, good job!
@DefogTech
@DefogTech 5 жыл бұрын
Thank you
@kakashihatake4840
@kakashihatake4840 6 жыл бұрын
Please do more such videos
@studio2038
@studio2038 4 жыл бұрын
nice explanation
@prasantkumar7693
@prasantkumar7693 5 жыл бұрын
Thank you for nice explanation and make more videos.
@Lucky-uz3je
@Lucky-uz3je 5 жыл бұрын
You are my interview trainer...love you bro:)
@krishnach7290
@krishnach7290 4 жыл бұрын
At 7:01 in video, x is volatile. But not a b or c. Will they also pushed back to main memory along with x? later in the same video you showing synchronise / locks for all fields
@shobhitmittal77
@shobhitmittal77 4 жыл бұрын
This is one ultimate video...subscribed !!
@skaLife
@skaLife 4 жыл бұрын
You have a good teacher in you. Please continue the good work.
@angelakarapetyan586
@angelakarapetyan586 3 жыл бұрын
I cannot understand one thing, how can synchronized work without volotile correctly, Does not thread copy x only to it's memory anyway?
@TauCu
@TauCu 4 жыл бұрын
This helped me understand a lot about the JVM Thanks, you're very good at explaining things without any unneeded gibberish!
@philthehuman
@philthehuman 6 жыл бұрын
Very clear video, though I would like suggest you to fix the code examples of locks or at least to have a disclaimer by voice that no one should use locks by your examples and should read a documentation instead which says you have to use "try { .. } finally { lock.unlock(); }" construct.
@DefogTech
@DefogTech 6 жыл бұрын
Agreed. Will add a card to the video. Thanks!
@pitchireddyb646
@pitchireddyb646 3 жыл бұрын
Volatile and with out Volatile keyword example differences am not getting clearly. Can some one explain and post here
@monstersmiled648
@monstersmiled648 4 жыл бұрын
Hi can you help me I am confused when i trying the output as per the diagram in the time 00:03:52 here i used this... System.out.println("the writerThread x is: "+x); System.out.println("the readerThread r2 is:"+r2); --------------------------------------------------------------------------------------------------------------------------------- like in this program(below): public class FieldVisibility { int x = 0; public void writerThread() { x = 1; System.out.println("the writerThread x is: "+x); } public void readerThread() { int r2 = x; System.out.println("the readerThread r2 is:"+x); } } output is:-- the writerThread x is: 1 the readerThread r2 is:1 ....................................................................................... instead of the writerThread x is: 1 the readerThread r2 is:0
@StyleTrick
@StyleTrick 6 жыл бұрын
Great video! Would it be possible to showcase when this happens? I've tried to replicate the issue with creating 2 new threads, however they seem to read x perfectly fine.
@DefogTech
@DefogTech 6 жыл бұрын
Sure, so it is possible that JVM will show the updated value to the other thread even though it is not say volatile, but, it is not guaranteed. In certain cases it can choose to keep using cached value. With volatile and other JMM rules we can be 100% sure that read-after always works. Can you share your code please. I can take a look
@StyleTrick
@StyleTrick 6 жыл бұрын
public class JavaMemoryModel { static int x = 0; public static void writerThread() { x = 11; System.out.println("In writer thread: " + Thread.currentThread().getName() + ": " + x); } public static void readerThread() { int r2 = x; System.out.println("In reader thread: " + Thread.currentThread().getName() + ": " + r2); } public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(6); executorService.execute(new Runner()); executorService.execute(() -> readerThread()); executorService.execute(() -> writerThread()); executorService.execute(new Runner()); executorService.execute(() -> readerThread()); executorService.execute(() -> writerThread()); executorService.shutdown(); } static class Runner implements Runnable { @Override public void run() { x = 29; System.out.println("In Runner Class: " + Thread.currentThread().getName() + ": " + x); } } } Something like this. How would you replicate the error of the shared cache not being updated?
@DefogTech
@DefogTech 6 жыл бұрын
Check this code gist.github.com/DeepakVadgama/fdd282d9b10794ed21867dc912bafffd Try calling reader and writer methods, instead of reader2 and writer2. One uses atomicInteger, other uses int. In the output, int shows inconsistent results, while atomicInteger one prints in correct order.
@anilkurmi5966
@anilkurmi5966 3 жыл бұрын
please stop making such Video, it will make everyone java expert :)
@DrAkyla1123
@DrAkyla1123 6 жыл бұрын
Very concise and easy to grasp. Great job!
@vikramreddy7586
@vikramreddy7586 4 жыл бұрын
This video is pure GOLD!!!
@kakashihatake4840
@kakashihatake4840 6 жыл бұрын
Really loved the explanation. Thank you
@srinathchintha9565
@srinathchintha9565 2 жыл бұрын
Hi why did you stop making videos.. please make more videos..
@adamrizk6735
@adamrizk6735 2 ай бұрын
What an excellent presentation, thank you for sharing
@anilkinikar
@anilkinikar 5 жыл бұрын
Awesome clarity. Voice is very clear and videos are well designed. Subscribed 👍
@mostinho7
@mostinho7 4 жыл бұрын
2:10 each core has its own cache and then there are shared caches. Thread running on core changes the variable in its local cache only, volatile keyword makes sure variable is flushed to shared cache when thread changes it so that changes are visible to all the other threads 6:05 happens before relationship 9:30 volatile keyword example
@monstersmiled648
@monstersmiled648 4 жыл бұрын
Hi can you help me I am confused when i was trying real time as per the diagram in the time 00:03:52 here i used this... System.out.println("the writerThread x is: "+x); System.out.println("the readerThread r2 is:"+r2); --------------------------------------------------------------------------------------------------------------------------------- like in this program(below): public class FieldVisibility { int x = 0; public void writerThread() { x = 1; System.out.println("the writerThread x is: "+x); } public void readerThread() { int r2 = x; System.out.println("the readerThread r2 is:"+x); } } output is:-- the writerThread x is: 1 the readerThread r2 is:1 ....................................................................................... instead of the writerThread x is: 1 the readerThread r2 is:0
@edeepakjain
@edeepakjain 5 жыл бұрын
Best explanation of volatile so far... 👌
@chamlyidunil
@chamlyidunil 4 жыл бұрын
how we can test compiler optimise our code. Ex : a = 3; b = 2; a = a + 1 to a = 3; a = a + 1; b = 2 i wrote this code, compiled and check class file. but when it decompile same instruction appeared. so how we can prove that, compier optimise our code.
@uka0002
@uka0002 4 жыл бұрын
Great video
@jiteshkumar3387
@jiteshkumar3387 3 жыл бұрын
Good explanation !! :) Very usefull
@jeremythen7239
@jeremythen7239 5 жыл бұрын
Best explanations! Keep it up.
@hbhatia999
@hbhatia999 Жыл бұрын
@DefogTech At time 8:47 How we are saying that value of all 4 variable will get updated while reading in readerThread because You are saying that X is not volatile. ?? As per first video of Volatile ,In this it was said that volatile values gets updated in Cache and because of that we are able to get updated value in different thread. Please help in this..
@DefogTech
@DefogTech Жыл бұрын
Updates to variables are visible to other threads by using any one of the options, volatile, synchronized or locks. Thus if you are using synchronized, even if variable is not volatile, updates will still be visibile to other threads.
@ghoshsuman9495
@ghoshsuman9495 3 жыл бұрын
6:55 only case of volatile and synchronization/intrinsic lock
@Admiral.MacDonnell
@Admiral.MacDonnell 8 ай бұрын
My takeaway - if the tasks perform blocking IO operation, I should not use fork join pool. so true. Thanks for the explanation! You have a gift of mixing complicated concept easy to digest!
@TTomni
@TTomni 4 жыл бұрын
I think that the example with "a,b,c,x" is wrong. The program order is not the code order thus d1, d2 and d3 can be zeroes even when "x" is synchronized.
@DefogTech
@DefogTech 4 жыл бұрын
As per the JVM specification, any variables written to before synchronization by one thread, will be visible (updated values) to other threads using same synchronization. Thus even if a,b,c are not used for synchronization itself, their updated values are still visible to the other thread
@narendrasriram
@narendrasriram 9 ай бұрын
This playlist you have made is too awesome. your example/explanation for happens-before has a few issues. Not sure if I understood wrong, otherwise.Thank you so much for this playslit.
@sharadrsoni1991
@sharadrsoni1991 4 жыл бұрын
Late comer :) Question: How at 9:03 locks will result in maintaining the variables a,b,c,x being atomic? The locks are for methods, which I understand as, no other there can enter in THAT method, but if some other methods are executed, then you may get the stale data. Isn't it?
@aniln4736
@aniln4736 4 жыл бұрын
In my case there is concurrent hashmap obj that holds configurations read from different server. Quartz scheduler will fetch configuration data for every 30 sec and update a new data (whole obj) to this concurrent hashmap.. In this case ,in my opinion voltaile should be used to always get the updated obj to the reader threads but some quality checking tools raising it as bug So any suggestions during this scenario
@Murga_Mutton
@Murga_Mutton 4 жыл бұрын
Hi, Thanks for the video. How is the volatile or synchronized or lock usage different from having the field in the constructor? Can't the shared cache object be used by putting the field in the constructor method and calling it in different threads?
@sbylk99
@sbylk99 6 жыл бұрын
Great tutorial, thank you SIR!
@DefogTech
@DefogTech 6 жыл бұрын
You're very welcome!
@sivagurunathanparanitharan4714
@sivagurunathanparanitharan4714 4 жыл бұрын
🙏 You are the Best Teacher that I have ever seen.🥇, A classical method of approach = Superb indeed.👌.🎯Please Could you kindly teach us Flutter & Dart & Go lang & Scala too . Pleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeese Sir..... 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
@isayanpal
@isayanpal Жыл бұрын
You're just brilliant! 🙏🏽
@khushbuchoudhary5185
@khushbuchoudhary5185 3 жыл бұрын
Super video! I applauded for ₹40.00 👏
@noortech254
@noortech254 3 жыл бұрын
Bro You are simply amazing. You may be not realised how much you are helping us. Thank You bhai :D
@AmitGadaley17
@AmitGadaley17 3 жыл бұрын
Hi, Could you please make a video on memory management in java or provide a useful link for it. Thanks!!
@abhinavsrivastava1870
@abhinavsrivastava1870 5 жыл бұрын
Can Volatile keyword be used with HashMap in any where code snippet. If yes, how to do that?
@buddu4798
@buddu4798 4 жыл бұрын
Dude .. very nice explanation. Can you provide code for reproduce same. How to control thead to be run the only of method. Provide code
@sshks10
@sshks10 3 жыл бұрын
Very good and simple explanation
@perrym8048
@perrym8048 9 ай бұрын
Best explanation I've seen, great work!
@AbhishekKumar-fs9nt
@AbhishekKumar-fs9nt 5 жыл бұрын
This is something similar to how computer hardware architecture is designed using pipeline. Nothing different from that.
@javawithyousuf192
@javawithyousuf192 8 ай бұрын
@kannadasans7564
@kannadasans7564 4 жыл бұрын
Could you make a samples of reactive programming followed by your aka Java fiber videos. For eg, How to use RxJava for the below categories of apps, 1. Considering both frontend and backend 2. Considering there is no frontend and it's purely backend app. Doing good job. Really great👍
@harshitshukla7382
@harshitshukla7382 6 жыл бұрын
Can you make a video on stream api in java8? I am very much looking forward to understanding them and you have an excellent presentation and organization of the videos. Keep doing the good job!!
@DefogTech
@DefogTech 6 жыл бұрын
Thank you sir! Stream API is on my list, will definitely upload one in coming weeks
@__nitinkumar__
@__nitinkumar__ 2 жыл бұрын
2:50 loved this diagram man.
@kotojava
@kotojava 2 жыл бұрын
this channel deserve much more subscribers.
@loveutube04
@loveutube04 3 жыл бұрын
Who the fuck dislikes a video like this?
@Chekist2008
@Chekist2008 4 жыл бұрын
Does volitate mean value of x will be changed on shared cache or change it in local and only after instantly flush to shared?
@DefogTech
@DefogTech 4 жыл бұрын
I dont know if changes are instantly flushed, or lazily flushed when different thread also asks for same variable.
@dinkarinjosh
@dinkarinjosh 4 жыл бұрын
can you please make a video on working of JVM and its architecture? btw good job.
@shilpaagrawal7126
@shilpaagrawal7126 4 жыл бұрын
Awesome video... Can u also make video on design patterns ...?
@hippiestafarii
@hippiestafarii 4 жыл бұрын
Thank for sharing :-)
@manognajoshik8465
@manognajoshik8465 2 жыл бұрын
Thank you very much sir!! You are amazing ❤️
@vijaykumarreddyt3287
@vijaykumarreddyt3287 Жыл бұрын
You are doing Great! Dude
@SatyaprasadMr
@SatyaprasadMr 4 жыл бұрын
Videos are very useful. Crisp & Clear. I request to make more videos
@sriram6148
@sriram6148 2 жыл бұрын
Amazing!!! You rock!
@Jayantch.999
@Jayantch.999 3 жыл бұрын
Super explanation 👍
@gauravbajpai6890
@gauravbajpai6890 5 жыл бұрын
U earned a fan today - Subscribed !
@andrii5866
@andrii5866 3 жыл бұрын
Awesome. Thanks man!
@kamilwrobel4438
@kamilwrobel4438 3 жыл бұрын
So if two threads are runing in one core, there is no need to use volatile keyword because they share same cache?
@abyssalwitch-b3h
@abyssalwitch-b3h Ай бұрын
yes but very difficult to do it
@BeatSyncBytes
@BeatSyncBytes 4 жыл бұрын
Explaination with L2 and L3 is so deep. How do you get so deep
@satyaprasad9615
@satyaprasad9615 4 жыл бұрын
Can you please provide the example complete code for this session
@eshagupta9407
@eshagupta9407 4 жыл бұрын
plz do more videos!!!
@paneerdopyaaza2914
@paneerdopyaaza2914 4 жыл бұрын
Please make a whole course...I will definitely try buying it.Your videos are just awesome.
@hakura88
@hakura88 Жыл бұрын
love your simple examples. thank you
@anugrah.singhal
@anugrah.singhal 3 жыл бұрын
Very good content 😊
@prashanttiwari120
@prashanttiwari120 4 жыл бұрын
Best video on java on whole KZbin universe
@MarkGla55
@MarkGla55 3 жыл бұрын
Excellent explanation. Thank you
@ivanevseevv
@ivanevseevv 3 жыл бұрын
Explanation is very good and sharp!
Java ExecutorService - Part 1 - Introduction
10:12
Defog Tech
Рет қаралды 417 М.
The Java Memory Model - The Basics
23:41
Jakob Jenkov
Рет қаралды 128 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 24 МЛН
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 16 МЛН
Every parent is like this ❤️💚💚💜💙
00:10
Like Asiya
Рет қаралды 17 МЛН
Memory Fundamentals - part 1 of Java Memory Management
18:31
Virtual Pair Programmers
Рет қаралды 144 М.
The Java memory model explained, Rafael Winterhalter
48:01
Bulgarian Java User Group
Рет қаралды 19 М.
14. JAVA. Memory Model | Технострим
30:20
VK Team
Рет қаралды 41 М.
Алексей Шипилёв - ForkJoinPool в Java 8
1:40:01
JPoint, Joker и JUG ru
Рет қаралды 74 М.
Introduction to CompletableFuture in Java 8
19:34
Defog Tech
Рет қаралды 326 М.
ThreadLocal in Java
10:59
Defog Tech
Рет қаралды 185 М.
Урок Java 346: Volatile и happens before
18:16
Уроки Java
Рет қаралды 12 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 24 МЛН