Java Memory Model in 10 minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ต.ค. 2024

ความคิดเห็น • 205

  • @70ME3E
    @70ME3E 4 ปีที่แล้ว +86

    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 ปีที่แล้ว +5

      Wow, thanks!

    • @mohammedrahamathulla4040
      @mohammedrahamathulla4040 3 ปีที่แล้ว +9

      @@DefogTech Why no videos from you😔?? All your subscribers are waiting

  • @monishchhadwa777
    @monishchhadwa777 8 หลายเดือนก่อน +2

    I would absolutely recommend all Java engineers-whether junior or very senior-to watch this!

  • @shellindebted5328
    @shellindebted5328 6 ปีที่แล้ว +18

    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 ปีที่แล้ว +1

      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 .

  • @rohitsharma-xt8qe
    @rohitsharma-xt8qe 5 ปีที่แล้ว +8

    I have never seen such an excellent explanation of JMM. Thanks a lot

  • @suhani091088
    @suhani091088 4 ปีที่แล้ว +4

    Always read these topic therotically, but could never visualise it like this you have explained..Great presentation man!

  • @NaturalPro100
    @NaturalPro100 5 ปีที่แล้ว +24

    The best video i could find on youtube.

  • @Dedeepya9
    @Dedeepya9 5 ปีที่แล้ว +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 ปีที่แล้ว +2

      Thank you for the kind words!! I am so happy its helping everyone

    • @sepraveen
      @sepraveen 4 ปีที่แล้ว

      Yes are techie by heart

  • @bhavyashah2630
    @bhavyashah2630 6 ปีที่แล้ว +60

    Awesome video...Please don't stop making such excellent videos.

  • @Admiral.MacDonnell
    @Admiral.MacDonnell 9 หลายเดือนก่อน

    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!

  • @iknowyourerightbut6246
    @iknowyourerightbut6246 5 ปีที่แล้ว +7

    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.

  • @tanoysaha7410
    @tanoysaha7410 5 ปีที่แล้ว +2

    Your explanations are precise, but have so in-depth/hidden informations and moreover so easy to understand

  • @Machinerium
    @Machinerium 5 ปีที่แล้ว +9

    I had an interview question like that.. This is a very clean explanation , keep doing it!

  • @mrchief3383
    @mrchief3383 4 ปีที่แล้ว +5

    Man, you are awesome. I was struggling trying to understand atomic operations and the use cases of volatile variables. Thank you so much

  • @TauCu
    @TauCu 4 ปีที่แล้ว +1

    This helped me understand a lot about the JVM Thanks, you're very good at explaining things without any unneeded gibberish!

  • @skaLife
    @skaLife 4 ปีที่แล้ว +1

    You have a good teacher in you. Please continue the good work.

  • @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.

  • @prashanttiwari120
    @prashanttiwari120 4 ปีที่แล้ว

    Best video on java on whole TH-cam universe

  • @paneerdopyaaza2914
    @paneerdopyaaza2914 4 ปีที่แล้ว

    Please make a whole course...I will definitely try buying it.Your videos are just awesome.

  • @kotojava
    @kotojava 2 ปีที่แล้ว

    this channel deserve much more subscribers.

  • @StyleTrick
    @StyleTrick 4 ปีที่แล้ว +3

    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?

  • @rajasruji2476
    @rajasruji2476 3 ปีที่แล้ว

    Wow…. Such an well articulated video. I felt this topic to be very difficult to understand before I watched your video. Please sir keep adding more and more topics to this channel. For the first time fell in love with subject videos.

  • @harish7706
    @harish7706 5 ปีที่แล้ว

    I have been going through a lot of JAVA topic videos in different forums, by far Defog Tech is the best that I have come across. Advanced topics explained in a simple way which provides clarity. Keep going

    • @DefogTech
      @DefogTech  5 ปีที่แล้ว

      Thank you for the kind words sir!

  • @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..... 🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

  • @vikramreddy7586
    @vikramreddy7586 4 ปีที่แล้ว +1

    This video is pure GOLD!!!

  • @sumitbabel5415
    @sumitbabel5415 3 ปีที่แล้ว

    Honestly exceptionally well explained Sir, hats off

  • @contactdi8426
    @contactdi8426 2 ปีที่แล้ว +1

    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.

  • @mostinho7
    @mostinho7 4 ปีที่แล้ว +1

    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

  • @VikiSangani
    @VikiSangani 5 ปีที่แล้ว +2

    Very nicely explained. Please keep making such videos.👍👍

  • @Lucky-uz3je
    @Lucky-uz3je 5 ปีที่แล้ว +3

    You are my interview trainer...love you bro:)

  • @perrym8048
    @perrym8048 10 หลายเดือนก่อน

    Best explanation I've seen, great work!

  • @noortech254
    @noortech254 3 ปีที่แล้ว

    Bro You are simply amazing. You may be not realised how much you are helping us. Thank You bhai :D

  • @anilkinikar
    @anilkinikar 5 ปีที่แล้ว +1

    Awesome clarity. Voice is very clear and videos are well designed. Subscribed 👍

  • @saravanprathi6956
    @saravanprathi6956 3 ปีที่แล้ว +1

    You are a fantastic teacher.. please please make more videos

  • @SatyaprasadMr
    @SatyaprasadMr 4 ปีที่แล้ว

    Videos are very useful. Crisp & Clear. I request to make more videos

  • @shaikshasayyad131
    @shaikshasayyad131 5 ปีที่แล้ว +4

    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 ปีที่แล้ว +2

      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.

  • @ganeshpathak4804
    @ganeshpathak4804 5 ปีที่แล้ว +2

    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

  • @DrAkyla1123
    @DrAkyla1123 6 ปีที่แล้ว +3

    Very concise and easy to grasp. Great job!

  • @adamrizk6735
    @adamrizk6735 2 หลายเดือนก่อน

    What an excellent presentation, thank you for sharing

  • @edeepakjain
    @edeepakjain 5 ปีที่แล้ว +1

    Best explanation of volatile so far... 👌

  • @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

  • @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?

  • @vikrantharne8576
    @vikrantharne8576 5 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      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

  • @purnimadas3762
    @purnimadas3762 5 ปีที่แล้ว +4

    That was really good. Thanks for posting, excellent, clear and to the point presentation, good job!

  • @philthehuman
    @philthehuman 6 ปีที่แล้ว +1

    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!

  • @ivanevseevv
    @ivanevseevv 3 ปีที่แล้ว

    Explanation is very good and sharp!

  • @venu5791
    @venu5791 4 ปีที่แล้ว

    Thanks for sharing this concept. I had only some theoretical knowledge of this before I watch this imp video.

  • @thomashpfner-dahl1433
    @thomashpfner-dahl1433 5 ปีที่แล้ว

    Dude!! You are awesome, a true lifesaver. You are the first that have made it simple, at least that I have come across

    • @DefogTech
      @DefogTech  5 ปีที่แล้ว

      Thanks much buddy!

  • @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?

  • @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?

  • @pradhumansingh1195
    @pradhumansingh1195 3 ปีที่แล้ว

    Who the hell dislikes such a great videos??

  • @viveksingh9120
    @viveksingh9120 5 ปีที่แล้ว +5

    Awesome mate. You are really doing great, very impressive

  • @shobhitmittal77
    @shobhitmittal77 4 ปีที่แล้ว +1

    This is one ultimate video...subscribed !!

  • @tarang65
    @tarang65 3 ปีที่แล้ว

    Amazing presentation. All the videos are really helpful. Great work.

  • @abhinavsrivastava1870
    @abhinavsrivastava1870 5 ปีที่แล้ว

    Can Volatile keyword be used with HashMap in any where code snippet. If yes, how to do that?

  • @sandeepkumarsahoo3458
    @sandeepkumarsahoo3458 5 ปีที่แล้ว

    Crystal clear explaination... Loved 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

  • @nlarralde
    @nlarralde 3 ปีที่แล้ว

    Great video, very clear explanation and examples. Thank you

  • @niftymiller6057
    @niftymiller6057 6 ปีที่แล้ว +4

    Very good and crisp way of explanation. Keep doing this good :) work

  • @coreyhartman4510
    @coreyhartman4510 5 ปีที่แล้ว +2

    This video is excellent.

  • @chetankhandave1072
    @chetankhandave1072 5 ปีที่แล้ว

    very very good explanation, it shows you have studied very hard , Hats off to you.
    But I will be very happy if you provide good working example to show difference between with volatile and without volatile,
    because when I try to find out difference using volatile and without volatile I do not notice anyone. your example in video do not demonstrate difference when I try it myself.
    thanks

    • @DefogTech
      @DefogTech  5 ปีที่แล้ว +1

      Correct, so without volatile JVM doesn't guarantee other threads will see updated value. But it depends on speed of your operations, JVM implementation etc. Thus like for most concurrency issues it will work on local, will work in production for few days but might fail suddenly after

    • @chetankhandave1072
      @chetankhandave1072 5 ปีที่แล้ว +2

      Yes you are correct, when I run my program again and again so many times then at some time situation arises and my volatile variable and non volatile variable get different values.
      I will be happy to share program to you.
      thanks

  • @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.

  • @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.

  • @sshks10
    @sshks10 3 ปีที่แล้ว

    Very good and simple explanation

  • @__nitinkumar__
    @__nitinkumar__ 2 ปีที่แล้ว

    2:50 loved this diagram man.

  • @ardiansyah6794
    @ardiansyah6794 4 ปีที่แล้ว

    Oh thanks man. I wish you were my lecturer 😀

  • @studio2038
    @studio2038 4 ปีที่แล้ว +1

    very simple to understand

  • @AmitGadaley17
    @AmitGadaley17 3 ปีที่แล้ว

    Hi, Could you please make a video on memory management in java or provide a useful link for it. Thanks!!

  • @BeatSyncBytes
    @BeatSyncBytes 4 ปีที่แล้ว

    Explaination with L2 and L3 is so deep. How do you get so deep

  • @debabratapal8060
    @debabratapal8060 4 ปีที่แล้ว

    Hi dude, Love your explanations. I have a doubt though. The visibility problem that you have mentioned in this video using the boolean flag- my question is from that area. Is the problem of visibility in the flag question a mere chance or is a must case. Like if I don't use volatile in the flag, is it like in every scenario my code will get into an infinite loop in thread 1, despite the update done by thread 2 on the flag; or is it like may happen in certain scenarios and may not happen in others?
    Thanks for your clear explanations.

    • @DefogTech
      @DefogTech  4 ปีที่แล้ว +2

      The latter.. it may or may not be visible

    • @debabratapal8060
      @debabratapal8060 4 ปีที่แล้ว

      @@DefogTech thanks man.....great content....love ur explanations

  • @hakura88
    @hakura88 ปีที่แล้ว

    love your simple examples. thank you

  • @StyleTrick
    @StyleTrick 6 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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.

  • @RobinsonGames
    @RobinsonGames 4 ปีที่แล้ว +1

    Really good explanation with a good visual display of those

  • @khushbuchoudhary5185
    @khushbuchoudhary5185 3 ปีที่แล้ว

    Super video! I applauded for ₹40.00 👏

  • @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👍

  • @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 ปีที่แล้ว +1

      I dont know if changes are instantly flushed, or lazily flushed when different thread also asks for same variable.

  • @MarkGla55
    @MarkGla55 3 ปีที่แล้ว

    Excellent explanation. Thank you

  • @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 2 หลายเดือนก่อน

      yes but very difficult to do it

  • @exploretheworld8208
    @exploretheworld8208 4 ปีที่แล้ว

    Hi I tried this fuctionality as you explained , Even if i keep as regular boolean instead of adding volatile, still the execution stops at some point. It does not run forever. Below is the example. Can you please explain why.
    package java8.labdaexp;
    public class VoltatileVisibility {
    boolean flag=true;
    public void writerThread() {
    System.out.println("Write operation");
    flag =false;
    }

    public void readerThread() {
    while (flag) {
    System.out.println("Read operation");
    }
    }
    }

    • @preetomsahaarko8145
      @preetomsahaarko8145 4 ปีที่แล้ว +3

      It may or may not happen. We cannot control the flush if we don't use volatile keyword. But if we use volatile keyword, it's guaranteed that the update will be flushed.

    • @DefogTech
      @DefogTech  4 ปีที่แล้ว +2

      in most cases even without volatile the value is updated, but as per the spec JVM can choose to not show updates to other threads for perf or similar reasons. Using volatile is a guarantee given by spec itself.

    • @exploretheworld8208
      @exploretheworld8208 4 ปีที่แล้ว

      ​ Defog Tech Thanks for the explaination !!!

    • @exploretheworld8208
      @exploretheworld8208 4 ปีที่แล้ว

      @@preetomsahaarko8145 Got it. Thanks !!!

  • @mujahidshaikh5034
    @mujahidshaikh5034 3 ปีที่แล้ว

    everything to the point.. love ur videos bro❤️

  • @gauravbajpai6890
    @gauravbajpai6890 5 ปีที่แล้ว +1

    U earned a fan today - Subscribed !

  • @isayanpal
    @isayanpal ปีที่แล้ว

    You're just brilliant! 🙏🏽

  • @gurupadamgundala5142
    @gurupadamgundala5142 6 ปีที่แล้ว +4

    Awesome. Expected more videsos 👌👌👌

    • @DefogTech
      @DefogTech  6 ปีที่แล้ว +2

      Thank you sir!

  • @masteradityamishra9486
    @masteradityamishra9486 6 ปีที่แล้ว +2

    Very nice explanation appreciate it, thanks.

  • @pkbkarpr
    @pkbkarpr 4 ปีที่แล้ว

    Short and Sweet

  • @abhik9450
    @abhik9450 5 ปีที่แล้ว

    Thanks for a proper explanation on happens-before 🙂

  • @manognajoshik8465
    @manognajoshik8465 2 ปีที่แล้ว

    Thank you very much sir!! You are amazing ❤️

  • @kakashihatake4840
    @kakashihatake4840 6 ปีที่แล้ว +3

    Really loved the explanation. Thank you

  • @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

  • @umeriqbal5662
    @umeriqbal5662 5 ปีที่แล้ว

    Jigger u r love...
    Awesome video....
    Amazing teaching skills... Thanks a lot

  • @AkshayNalavade18
    @AkshayNalavade18 3 ปีที่แล้ว +1

    Volatile won't work here. You have to use Atomic integer.... You have explained it in another of your videos.
    Volatile vs Atomic

  • @pogrommist
    @pogrommist 4 ปีที่แล้ว

    Awesome explanation and examples!

  • @mahendrakhedhar6054
    @mahendrakhedhar6054 6 ปีที่แล้ว

    read write barrier means what sir please explain if we use volatile what happens to those barriers

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 ปีที่แล้ว

    Thank u, waiting for more videos on java

  • @jiteshkumar3387
    @jiteshkumar3387 3 ปีที่แล้ว

    Good explanation !! :) Very usefull

  • @prasantkumar7693
    @prasantkumar7693 5 ปีที่แล้ว +2

    Thank you for nice explanation and make more videos.

  • @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 ปีที่แล้ว +1

      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

  • @shilpaagrawal7126
    @shilpaagrawal7126 4 ปีที่แล้ว

    Awesome video... Can u also make video on design patterns ...?

  • @pitchireddyb646
    @pitchireddyb646 3 ปีที่แล้ว

    Volatile and with out Volatile keyword example differences am not getting clearly. Can some one explain and post here

  • @jeremythen7239
    @jeremythen7239 5 ปีที่แล้ว +2

    Best explanations! Keep it up.

  • @vijaykumarreddyt3287
    @vijaykumarreddyt3287 ปีที่แล้ว

    You are doing Great! Dude