13.7 Multithreading Synchronized Keyword

  Рет қаралды 431,353

Telusko

Telusko

7 жыл бұрын

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.
Multithreading in java is a process of executing multiple activities can proceed concurrently in the same program.
Thread is basically a lightweight sub-process, a smallest unit of processing.
In multithreading threads share a common memory area.
They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
Using two different task at the same time means multi-tasking. Thread is unit of a process.
Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Check out our website: www.telusko.com
Follow Telusko on Twitter: / navinreddy20
Follow on Facebook:
Telusko : / teluskolearnings
Navin Reddy : / navintelusko
Follow Navin Reddy on Instagram: / navinreddy20
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi :
/ @teluskohindi
Subscribe to the channel and learn Programming in easy way.
Java Tutorial for Beginners : goo.gl/p10QfB
Scala Tutorials for Java Developers : goo.gl/8H1aE5
C Tutorial Playlist : goo.gl/8v92pu
Android Tutorial for Beginners Playlist : goo.gl/MzlIUJ
XML Tutorial : goo.gl/Eo79do
Design Patterns in Java : goo.gl/Kd2MWE
Socket Programming in Java : goo.gl/jlMEbg
Spring MVC Tutorial : goo.gl/9ubbG2
OpenShift Tutorial for Beginners : goo.gl/s58BQH
Spring Framework with Maven : goo.gl/MaEluO
Sql Tutorial for Beginners : goo.gl/x3PrTg
String Handling in Java : goo.gl/zUdPwa
Array in Java : goo.gl/uXTaUy
Java Servlet : goo.gl/R5nHp8
Exception Handling in Java : goo.gl/N4NbAW

Пікірлер: 228
@kartzoftvasu3162
@kartzoftvasu3162 3 жыл бұрын
those trying to test this programs on your high end laptops, might see no matter u use a counter of 1000 or 5000 or even 10,000 to increment, you might still get the correct total of incremented value without even using SYNCHRONIZE keyword to sync the increment process between 2 threads. It just means your thread scheduler has a lot of resources available to be very efficient. In such cases, just increase the counter loop by a million (1,00,000) and then you will see the need of using the SYNCHRONIZE keyword as you will see an incorrect total. Hope it helps.
@vishalramoju5432
@vishalramoju5432 3 жыл бұрын
thanks mate
@kumaraveld6126
@kumaraveld6126 2 жыл бұрын
Thanks :)
@cornel0001
@cornel0001 Жыл бұрын
Another hint here: remove any "per iteration" console log. While in loop, let only the counting, otherwise writing to console will give enough time to threads so they behave almost "synchronized"
@sidharthbisoi1492
@sidharthbisoi1492 23 күн бұрын
Try writing it as this Thread1.start(); Thread1.join(); Thread2.start(); Thread2.join(); Result: if you took for loop for 1000 or 1000000 each = 2000 or 2000000 respectively Without synchronised key word
@FL-xc1wk
@FL-xc1wk 22 күн бұрын
​@@sidharthbisoi1492simply adding synchronized diminishes the need for thread.join. however both things do the same
@avahome5285
@avahome5285 2 жыл бұрын
Notes: not synchronized method is not thread safe, which means multiple thread can access the same method at the same time. Making the thread synchronized means only one thread can use the method at a time.
@andrejtrozic509
@andrejtrozic509 3 ай бұрын
So if you want a method that can be used by one thread at a time -then make that method synchronized()
@redwanniloy2068
@redwanniloy2068 2 ай бұрын
@@andrejtrozic509 i was asked this question on an interview..its very imp
@FedoraTime
@FedoraTime 4 жыл бұрын
Very clear and simple example, thank you!
@uditswaroopa5809
@uditswaroopa5809 4 жыл бұрын
you cleared my doubt I did not skip any ad! Modern Guru Dakshina (and I learned If we don't skip first ad YT shows a second one)
@mohammedhammad2113
@mohammedhammad2113 3 жыл бұрын
That is a gem of an explanation, thank you Sir!
@mohammadarshad2908
@mohammadarshad2908 5 жыл бұрын
Thank you so much for creating such a useful video series ....it's been so useful for me...
@nynra6584
@nynra6584 7 жыл бұрын
Thank you for explaining it comprehensively.
@ashwanikumar4288
@ashwanikumar4288 5 жыл бұрын
explanation was crystal clear. Thanks a ton!
@ferfykins
@ferfykins 4 жыл бұрын
Excellent video, thanks so much, you made it easy and simple to understand :)
@rajag8851
@rajag8851 6 жыл бұрын
champav basu... intha simple ga telchesav, thread safe synchronization yappati nundo doubt. simply superb. hyd lo unte kaludam we can have good biryani..
@420thlegioner8
@420thlegioner8 4 жыл бұрын
Godlike explanation, CLEAR AND SIMPLE !
@shreyash8813
@shreyash8813 3 жыл бұрын
your videos are very good. perfect point-to-point explanation such as mechanical students are also getting interested.
@mohammedmoues5237
@mohammedmoues5237 2 жыл бұрын
Well explained and to the point thank you so much for the amazing video!
@ankitgoyal34
@ankitgoyal34 4 жыл бұрын
Excellent video, makes understanding very simple and easy!!
@sf2759
@sf2759 6 жыл бұрын
Crystal clear!!!much appreciated!
@pratimadaswante895
@pratimadaswante895 5 жыл бұрын
The ways of your explanation is awesome...😊
@suchitrajadhav1688
@suchitrajadhav1688 3 жыл бұрын
YOU are most talented navin.. best and simple way to understand that how u elaborate the concepts..
@gfunkonthecheesetree
@gfunkonthecheesetree 6 ай бұрын
Great video, thank you! One of the best Java instructors
@abdulqayyumisahak5190
@abdulqayyumisahak5190 7 жыл бұрын
good explanation. Thanks for the tutorial!
@sangavia6687
@sangavia6687 Жыл бұрын
Crisp and clear explanation!!
@titany679
@titany679 4 жыл бұрын
Thank you very much for this very informative and clear video. It helped me a lot :D
@PranathNaik
@PranathNaik 3 жыл бұрын
great sir very clear explanation one of the best multithreading explanation on KZfaq
@sravanisunkara3093
@sravanisunkara3093 3 жыл бұрын
wow! crystal clear explanation.THANK YOU!
@kacpermakuch7855
@kacpermakuch7855 6 жыл бұрын
I just love this intro !
@robertsedgewick1266
@robertsedgewick1266 3 жыл бұрын
Brilliant explanation, thanks for sharing!
@smithcodes1243
@smithcodes1243 4 жыл бұрын
Ahhhh Bossss! What an explanation!
@rajeshjain1204
@rajeshjain1204 4 ай бұрын
Best Java Teacher in KZfaq 💯
@moazelsawaf2000
@moazelsawaf2000 5 жыл бұрын
Thanks a lot
@shankerbabumothkuri3225
@shankerbabumothkuri3225 4 жыл бұрын
Naveen, You are an awesome teacher!!
@brucebane7401
@brucebane7401 3 жыл бұрын
absolute best !!!!!telusko🔥🔥🔥🔥
@saisrisai9649
@saisrisai9649 5 жыл бұрын
thank u so much sir . such a beautiful explanation
@bekjanomirzak224
@bekjanomirzak224 3 жыл бұрын
Thanks! great explanation on this topic!
@giorgi23
@giorgi23 5 жыл бұрын
Thanks a lot sir. You have very good explanation
@satishkrajendran8703
@satishkrajendran8703 7 жыл бұрын
Its really great. kudos to navin reddy
@anandjyoti3652
@anandjyoti3652 5 жыл бұрын
Thank You. Nicely explained
@isirasamarasinghe748
@isirasamarasinghe748 Жыл бұрын
this saved so much of my time! thank you so much!
@CoderSV3
@CoderSV3 4 жыл бұрын
Thank you very much. Now I understand!
@axdayrboom3284
@axdayrboom3284 5 жыл бұрын
thaaaaaaanks very much & I hope you'll explain the Thread pool
@deadlysk8
@deadlysk8 5 жыл бұрын
Great video, nice explanation
@aniruddhachunne3011
@aniruddhachunne3011 2 жыл бұрын
Awesomeee.... so concise and perfect explanation
@vengateshan7800
@vengateshan7800 8 ай бұрын
Awesome and very clear explanation and easy to understand
@hitanshimehta430
@hitanshimehta430 5 жыл бұрын
Thank you so much!!! It helped alot!!
@karthick2237
@karthick2237 5 жыл бұрын
Top class teaching ..👏👏👍
@raghulg6155
@raghulg6155 4 жыл бұрын
Damn cool explanation over this confusing topic....
@pratiklakade1062
@pratiklakade1062 Ай бұрын
What an explaination sir .Just Wow.
@darshankhatri7375
@darshankhatri7375 5 жыл бұрын
Sir you are Legend for CS Students.
@billyramogi8864
@billyramogi8864 6 жыл бұрын
Awesome tutorials
@lakshmanan932
@lakshmanan932 3 жыл бұрын
Great explanation....!!! Thank you.
@sanjaypatel-ux9zb
@sanjaypatel-ux9zb 7 жыл бұрын
nicely explained good example. thanx..
@maheshmahadar4780
@maheshmahadar4780 3 жыл бұрын
awesome explanation with example
@bharathrajvarma6011
@bharathrajvarma6011 4 жыл бұрын
very clear . thank you sir.
@simim4145
@simim4145 7 жыл бұрын
thank u soo much sir nice explanation...
@sairajdas6692
@sairajdas6692 5 жыл бұрын
Excellent explanation sir
@sergeyyakovlev4401
@sergeyyakovlev4401 6 жыл бұрын
It is a very good video. It helped me a lot.
@srikanthkolipaka2103
@srikanthkolipaka2103 5 жыл бұрын
Pretty clear 👍🏿👍🏿
@01234567899970
@01234567899970 2 жыл бұрын
thankyou so much for great explaination
@davidlira8853
@davidlira8853 2 жыл бұрын
Great example brother
@jakubsz4602
@jakubsz4602 2 жыл бұрын
thank you, all the best!
@iamtechboy3298
@iamtechboy3298 6 жыл бұрын
Thankss u made it much easy..
@jananiramesh5471
@jananiramesh5471 Жыл бұрын
Thank you sir really it was helped me to understand this concept clearly
@krishnamoorthysridhar8536
@krishnamoorthysridhar8536 11 ай бұрын
Good Explanation.. Thanks
@belikeprogrammer5237
@belikeprogrammer5237 7 жыл бұрын
nice explanation sir
@stiliyangoranov3015
@stiliyangoranov3015 6 жыл бұрын
AWESOME!!!!!!!!
@unbiased-review
@unbiased-review 6 жыл бұрын
Well explained sir.
@d-news4076
@d-news4076 2 жыл бұрын
Thanks! it helped me!
@dharmikbatra42
@dharmikbatra42 3 жыл бұрын
Bucky Roberts from India . Amazing tutorials sir!
@VaishaliGoyal49
@VaishaliGoyal49 6 жыл бұрын
Well explained
@cookwithme9623
@cookwithme9623 5 жыл бұрын
@Telusko We need teachers like you in college : )
@mayurpatilify
@mayurpatilify 5 жыл бұрын
Nice explained......
@PDSshinigami
@PDSshinigami 8 ай бұрын
very helpful but he took time trying to explain the logic that I see how people can get lost in the process ,but thnx for the syntax i think i get it
@vandadvafai48
@vandadvafai48 2 жыл бұрын
You saved me so much time...
@drunkscotsman2976
@drunkscotsman2976 6 жыл бұрын
great video
@lifeofme3172
@lifeofme3172 Жыл бұрын
Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method. A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource.
@neelancheri_yahoo
@neelancheri_yahoo 5 жыл бұрын
Very helpful video
@klemn0291
@klemn0291 Жыл бұрын
excellent video
@anelemadonda6191
@anelemadonda6191 3 жыл бұрын
best channel ever
@birtaprakashkundu7867
@birtaprakashkundu7867 6 жыл бұрын
Thanks sir.
@damianwojakowski3006
@damianwojakowski3006 4 жыл бұрын
This *synchronized* is a very big performance hit": *** mainThreadCall... Count: 2000000000 Done in: 125 milliseconds *** callingWithOneSeparateThread... Count: 2000000000 Done in: 129 milliseconds *** callingWithTwoThreads... (not synchronized method) Count: 1013362697 Done in: 67 milliseconds *** callingWithTwoThreadsAndSynchronizedMethod... Count: 2000000000 Done in: 67621 milliseconds Also on some conference I've seen an example where the "synchronized" did not help. I don't remember what was the case, but they had to do smething else. I think it was some I/O issue or with databases. Just a guess. Two synchronized threads are 540 times slower than one thread in my example (2,000,000,000 calls). But when there's less calls, it works fine (but still slower than in one thread).
@FungXianHu
@FungXianHu 5 жыл бұрын
thank you, sir
@adityaarya7294
@adityaarya7294 2 жыл бұрын
great!! thank you sir
@ahmeedev
@ahmeedev 3 жыл бұрын
Good job sir
@vivekmishra1520
@vivekmishra1520 3 жыл бұрын
thanks you for explain the concept very Cleary. I have one doubt , can we add volatile keyword to the count variable also and will it work simillar, If not can you please explain why?
@johnpaulduavis8097
@johnpaulduavis8097 4 жыл бұрын
Thanks Man
@shivam3839
@shivam3839 11 ай бұрын
nice video
@tanujarmy5300
@tanujarmy5300 5 жыл бұрын
Super 💙
@alittlecoding
@alittlecoding 3 жыл бұрын
You are the best
@srivaishnav2319
@srivaishnav2319 2 жыл бұрын
But if after using Synchronized both the processes cant simultaneously access the integer and increment then why even using multi-threading then, wouldn't it take the same time as a single-threaded process ?
@ahmeedev
@ahmeedev 3 жыл бұрын
i learnt three things 1. Synchronized keyword 2. join method 3. Thread safe class
@mounika.m8108
@mounika.m8108 3 жыл бұрын
Awesome...!!!
@gopalkannan4934
@gopalkannan4934 Жыл бұрын
Thank you sir
@NidhiKumari-ib6ow
@NidhiKumari-ib6ow Жыл бұрын
Thanks a lot of sir
@tanmaykhandelwal3705
@tanmaykhandelwal3705 4 жыл бұрын
Our aim for using multithreading was parallel computing, but here we are using a single thread at a time. So how are we achieving that?
@gowthamank6776
@gowthamank6776 6 жыл бұрын
well done super
@yashtibrewal4259
@yashtibrewal4259 6 жыл бұрын
Nice explanation. my.concepts++;
@edimarfsatumbo5675
@edimarfsatumbo5675 4 жыл бұрын
ty so much
@Pages_Perfected
@Pages_Perfected Жыл бұрын
A question, why you didn’t use implements or extends ? Thank you for the amazing video
@user-gt5si9py1y
@user-gt5si9py1y 11 ай бұрын
Because he didn't make new classes for custom threads, he used the classes Thread and Runnable themselves, a month late but well...
@ahalamma55
@ahalamma55 7 жыл бұрын
You said that synchronized methods block the method so only one Thread can use the method at the same time. What do I do if I'd like to block the whole Object while performing a method ..? For example Counter has a method called doMuchStuff which checks the count variable several times. While doing so, another thread could call increment and therefore the count value would change before doMuchStuff was even performed. Thank you very much, love your tutorials :)
@rahulgopalakrishnan1892
@rahulgopalakrishnan1892 2 жыл бұрын
t1.start(); t1.join(); t2.start(); t2.join(); will fetch the same result without using the synchronized key word. BUT...WHAT YOU HAVE DONE FOR THE PURPOSE OF EXPLAINING ITS A PERFECT EXAMPLE.
@alokyadav9684
@alokyadav9684 6 жыл бұрын
Awesome
@khushgandhi6638
@khushgandhi6638 7 жыл бұрын
its DT simple
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 72 МЛН
The joker's house has been invaded by a pseudo-human#joker #shorts
00:39
Untitled Joker
Рет қаралды 12 МЛН
Luck Decides My Future Again 🍀🍀🍀 #katebrush #shorts
00:19
Kate Brush
Рет қаралды 8 МЛН
I’m just a kid 🥹🥰 LeoNata family #shorts
00:12
LeoNata Family
Рет қаралды 18 МЛН
14.11 HashMap and HashTable in Java
8:44
Telusko
Рет қаралды 668 М.
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 885 М.
THREAD SYNCHRONIZATION - JAVA PROGRAMMING
43:08
Sundeep Saradhi Kanthety
Рет қаралды 107 М.
Java Tutorial - Synchronized methods
4:27
LinkedIn Learning
Рет қаралды 10 М.
Thread Safety in Java
9:13
Telusko
Рет қаралды 86 М.
Java multithreading 🧶
15:18
Bro Code
Рет қаралды 122 М.
Java threads 🧵
16:01
Bro Code
Рет қаралды 106 М.
Synchronized Keyword in Java | Tamil | code io
12:18
code io - Tamil
Рет қаралды 14 М.
Synchronization in Java Multithreading | Learn Coding
10:43
Learn Coding
Рет қаралды 269 М.
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 72 МЛН