Using volatile vs AtomicInteger in Java concurrency

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • When to use volatile, and when to use Atomic counterparts like AtomicInteger? What are the advantages and use cases of each?
    Channel
    ----------------------------------
    Complex concepts explained in short & simple manner. Topics include Java Concurrency, Spring Boot, Microservices, Distributed Systems etc. Feel free to ask any doubts in the comments. Also happy to take requests for new videos.
    Subscribe or explore the channel - / defogtech
    New video added every weekend.
    Popular Videos
    ----------------------------------
    What is an API Gateway - • What is an API Gateway?
    Executor Service - • Java ExecutorService -...
    Introduction to CompletableFuture - • Introduction to Comple...
    Java Memory Model in 10 minutes - • Java Memory Model in 1...
    Volatile vs Atomic - • Using volatile vs Atom...
    What is Spring Webflux - • What is Spring Webflux...
    Java Concurrency Interview question - • Java Concurrency Inter...

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

  • @bhaaratsharma6023
    @bhaaratsharma6023 3 ปีที่แล้ว +44

    Not saying for the sake of saying but after visiting a lot of other channels I always come back to your videos for anything related to multithreading. You are simply awesome.

  • @dig8195
    @dig8195 3 ปีที่แล้ว +47

    Hey man. I know this is already a 2year old video but I would like to express my thanks for putting these videos and I really learned a lot. Anything related to threads and concurrency in Java have been very intimidating for me and I always thought it's not important as I'm doing well in my current role in the company I'm working with. However, I realized my career as a software engineer is in rot due to not taking risks and for not taking on challenges by exploring what is out there for me.
    I applied for Canva(spoiler: didn't got the job) and since then, I started watching your videos and practice coding related to java concurrency because it was advised on their role description. I'm still brokenhearted by the feedback that I didn't pass their first coding exam, hence, was not able to apply what I've learned from you. I'm still very thankful for what I've learned from your videos and made me more interested in this part of java.
    Keep on making videos. Thanks!

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

      +1 prepping for Canva here. Coincidentally found this video and this comment.

  • @rohithp9774
    @rohithp9774 3 ปีที่แล้ว +15

    "The Best Content" for concurrency in java. Thanks much!

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

    Done thanks
    0:40 changing a variable in one thread will NOT reflect in the other thread. (Threads both have access to the variable, but each thread running on a different core the cpu has a local cache of the variable)
    2:30 volatile solves visibility issue but doesn’t solve synchronization issues (when both threads are modifying the same variable, both could increment but the value is only +1 instead of +2)
    4:50 solving synchronization with synchronized keyword
    5:15 atomic variables provide atomic operations (incrementong happens as one operation instead of read then increment then write, where both threads could be running together)
    6:30 which variables to make volatile and which values to make atomic

  • @Kp96321
    @Kp96321 6 ปีที่แล้ว +19

    Probably the most comprehensive explanation I've seen yet (recommend listening at 1.5x), thanks alot!

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

    After reading hundreds of resources finally I got the answer her. How to and where to use Volatile and Atomic variables.

  • @swapnilankalkhope1896
    @swapnilankalkhope1896 6 ปีที่แล้ว +21

    Clear and precise explanation...Best Ever!!! Plz Upload more core java concepts

  • @pandit-jee-bihar
    @pandit-jee-bihar 4 ปีที่แล้ว +3

    This is the best that you can do in 7 minutes to explain these relatively confusing concept.

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

    Perfect explanation!! I hope whoever watches this video, for sure they will get the idea of volatile and atomicInteger. Would you explain more about AtomicReference Class and its usage.

  • @yadavankit
    @yadavankit 4 หลายเดือนก่อน

    such a simple quick explanation. I keep coming on this channel for multi-threading concepts every once in a while

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

    Your videos are awesome .The lucid manner in which the concepts are explained is of the highest quality. Please continue making videos and never slow down.

  • @Lucky-uz3je
    @Lucky-uz3je 6 ปีที่แล้ว +54

    You have got one more subscriber.... Awesome explanation :)

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

      Thank you!

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

      count me in as well

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

      Metoo

    • @Lucky-uz3je
      @Lucky-uz3je 4 ปีที่แล้ว

      30 likes means, Defog Tech got 30 more subscriber. Awesome.
      You deserve more than that. Keep it up.😊👍

    • @Lucky-uz3je
      @Lucky-uz3je 4 ปีที่แล้ว

      Thanks for the 💓....
      But I have a complain from you. We, your subscribers, eagerly waits for your videos but it seems you are busy somewhere else. Please do your knowledge spread to us....😃
      Lots of love. Keep it up.

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

    i have 8 year exp and preparing for a major interview..all your videos are absolutely to the point. Cheers

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

    Short and Sweet

  • @YT-yt-yt-3
    @YT-yt-yt-3 3 ปีที่แล้ว

    Whenever I wanted refresh my java concurrency knowledge, this is the place I come in.

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

    Best Explanation, I have searched for volatile for so long, but understand here. Thanks

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

    2 main concepts explained in 7 minutes ,hatsoff!!

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

    This is one of the clearest explanations I got about volatile and Atomic on the internet. Well done.

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

    It could not be more simpler. Thanks a ton.

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

    This is by far the best explanation of synchronization I ever seen, thanks for that!

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

    I wish there is an option to like all the videos in the playlist at once. Amazing explanation keep going please!

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

    The best video about the keyword volatile!! Hands Down

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

    It might also help to share that volatile is enough if there is just one thread writing and multiple threads reading a variable. However, if more than one thread write, then we've those synchronize those.

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

    I never seen this type of video how crystally explain each concept through out the example u have really gud command of mutithreading 👍🙏

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

    really short and sweet,i don't have much clarity about volatile keyword i have searched many sites no one clearly explained like you really pictures,explanation,animations really very understandable way that for providing this vedio sir,expected more content like this sir.Thank you sirrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.................................................

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

      You are very welcome. I'm happy it helped!

  • @MohitKumar-os3ir
    @MohitKumar-os3ir 4 ปีที่แล้ว

    Every time I watch a video of you. Only one thought. This guy is a genius.

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

    Making everyone understand small things is not a small thing. Kudos to your way of explanation :)

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

    Excellent, you have nailed it, you explained it so well, so clearly, to the point. Appreciate you effort. Thanks a lot for posting it.

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

    "Best content on Java Advance Topics and Interview Questions" , Please Keep doing more such videos on microservices and AWS.

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

    Very simple and straight explanation good job

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

    Small comment, but quite useful - You don't have to mix volatile with synchronized, because synchronized already has a memory barrier that guarantees visibility of changes that were made within synchronized blocks of the same lock. "On the same lock" is the keyword here

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

    I was surfing for a very long time to understand it better . Your video was the best i have ever seen .

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

      Thank you! I'm happy it helped

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

    The overall content is perfect and to the point but request you if you can add a bit of thread creation and synchronization with thread in older fashion like efficient usage of notify and wait, it will help I am sure a bigger audience

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

    Huge Salute/respect to you sir. I always first like the video before starting and then start watching.

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

    Your explanation does clear all doubts

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

    such a crystal clear simple and short explanation.just waoo

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

    It couldn't have been explained any better..thanks a lot.

  • @tarun4705
    @tarun4705 6 หลายเดือนก่อน

    This playlist is a treasure

  • @pratapsingh-le2br
    @pratapsingh-le2br 5 ปีที่แล้ว

    i've never seen any tutorial regarding the multithreading in youtube like u i don't have any word what i said here really mesmerizing wd speechless bdw i am subscribing ur channel please upload the video Collection and OOps Concept and Exception Handling wd internal in depth wd relate the what ever u explain the concept real time like Banking domain and E Commerce

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

    Nice explanation, just to add to it: volatile is not required when u use synchronised block, as synchronised block provides visibility + atomicity

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

      Agreed.. good point

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

    very good explanation with very good example !!! I think I will never forget this concept now -Volatile/Synchronization and AtominInteger.

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

    The Best Playlist for Java Concurrency. Thanks.

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

    Thanks for the clear explanation on Volatile!

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

    Nice tutorial. It clears the concept of volatile and atomic integer

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

    Clear and concise videos. Enjoyed every single minute of this video. The clarity with which the concept is being explained is simply unparalleled. Thank you !!!

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

    Thanks for such wonderful videos.... you are the best in multithreading...

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

    Such an clear explanation ! Succcint and up to the point, before this no one really helped me understand the real implications of the volatile keyword, and many assumed volatile is the equivalent keyword of synchronized, for variables, but no, it is not !

  • @amanueletana
    @amanueletana 9 หลายเดือนก่อน

    you are amazing man. thank u. Indeed short and sweet.

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

    4:40 if you are sure all access to a variable (all reads and writes) are synchronized on the same object then you don’t need to make it volatile

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

      I am not a 100% sure on this, but maybe because there is no guarantee that the incremented value will be visible to the other thread. Just like in case of the boolean flag, we'll have the visibility problem in this case too.

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

      @@haaarshiiiit If both threads (writer and reader) synchronize on the same object the monitor will establish a happens after relationship which guarantees the change is visible (and also atomic). Same is true for some of the more advanced concurrent utilities (like CHM). Only if you don’t use any of them (or only in some places) you need to deal with the volatile mess.

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

    Fabulous explanation, I finally understood the AtomicInteger. Thanks!! ^^

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

    Today, I actually understood the difference between volatile and atomic variables

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

    all the essence about volatile, synchronized, atomic explained in simple effective way, beautiful :)

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

    Easy to understand, thank you very much, greeting from Indonesia

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

    short and sweet to the point, clear explanation,

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

    Finally I am able to comprehend atomic integer and its use... Thanks a lot! :)

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

    Finally I understood the concept - thank you immensely

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

    Very useful info ,Waiting for u r new videos

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

    Amazing explanation! Tbh one of the best explanation of volatile on the internet!

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

    Great Explanation..! look forward to more technical query explanations.

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

    Awesome explanation, clear voice, pronunciation, apt speech rate, no slow, no high. One question: What if all threads are running in parallel? Like parallel browsers? How can I make only one browser (thread) update the database?

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

    One of the best explanations I have ever heard. Man you are simply awesome. Straight to the point and very clear!!!!!!! 👏👏👏👏 Keep up the great work! I'm subscribed!

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

    Very crisp and clear content! Thank you, thank you !!😊

  • @24892xxx
    @24892xxx 5 ปีที่แล้ว +11

    sexy bro . I was thinking of starting my own channel with java tutorials , but now i ll will just give link to your channel whoever requires any guidance. Great work.

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

      I too share same feeling.. Initially I also wanted to have my own channel but why anybody needs other video.

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

    absolutely great. Paid course couldn't explain it in simple way.

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

    Great content, Just to updates/assignment that all primitives in java except float and double are already atomic so no need to add volatile in front of boolean/int etc.

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

    Nice Info, but mentioning on Compare and Swap (CAS) for Atomic operation would help your user to understand more and how optimistic locking works.

  • @ManishKumar-qx1kh
    @ManishKumar-qx1kh 4 ปีที่แล้ว

    I am late to this channel but i must say the explanations you give are awesome. Thanks for the hard work.

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

    You are really good on explaining complicated things.. I hope you make more video in java and spring, spring boot

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

    Thank you very nice GOD BLESS YOU your service is really great

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

    Thank you for the video. One question/clarification. I don't think we need put volatile if we are using synchronized method (4:53)

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

      true, as per Java Memory Model, variables written to within synchronized block are visible to all other threads using synchronization over same object/lock.

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

      Thanks. I was wondering about that also.

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

    Very useful illustration

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

    It helps me to understand in 2021..

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

    Wow, this is what I'm searching

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

    Very clear and good explanation of the topic, congrats 👏

  • @Rahul-co2zj
    @Rahul-co2zj 6 ปีที่แล้ว +4

    fantastic explanation !!
    keep it up brother....

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

    You're doing an amazing job. Thank you for all your efforts.

  • @manojsingh-xm5ph
    @manojsingh-xm5ph 6 ปีที่แล้ว

    Awesome !!!. Very nicely explation of each and every topics of channel uploaded videos.Want to see more and more topics on java concurrent api..Keep it up.

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

      Thank you for the kind words and your motivation sir!

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

    i am subscribed now after watching this video

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

    could nt be explained better , nice one bro!

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

    Just excellent 🙏. It will be helpful if you add some coding examples.

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

    Really nice way of explaining the concepts. Thanks

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

    So glad to have run into this. Great clarity.

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

    Very good. Teaching is an art & you definitely know it.

  • @ent.subirsaha
    @ent.subirsaha 4 ปีที่แล้ว

    You have explained the topic very nicely. Thanks. 👍

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

    Excellent explanation like always.. So articulate and to the point.. Cheers!!

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

    Very good and clear explanation, thanks.

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

    Thanks a lot sir. Great learning

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

    well explained, Thank You

  • @mehrdadbozorgmehr8501
    @mehrdadbozorgmehr8501 4 หลายเดือนก่อน

    very good explaination

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

    Hi Dude, can u please come back and start putting Videos again, i am missing your videos man. pls start some new thing like u started for NOSQL / Cloud / anything at-all i know u will rock in what ever u start 🙂

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

    well done! brief and up to the point

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

    Very nice way to explain such a complex topic.Thanks a lot.

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

    These video's are awesome. Please kindly make a playlist topic wise.

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

    very well explained, liked

  • @gurudatt-shahane
    @gurudatt-shahane ปีที่แล้ว

    Thank you very much for making this video ❤

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

    This video is gold.

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

    Great content, v easy to grasp, thanks for making these videos!!

  • @clayy.walker2108
    @clayy.walker2108 2 หลายเดือนก่อน

    Best youtuber i ever see

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

    Superb 👌 you explained it truely short n sweet way. Good good

  • @shubhamtandon5191
    @shubhamtandon5191 3 หลายเดือนก่อน

    Very nice, thanks bro