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.
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"
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
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.
champav basu... intha simple ga telchesav, thread safe synchronization yappati nundo doubt. simply superb. hyd lo unte kaludam we can have good biryani..
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.
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.
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).
In that case, its not multithreading because until t1 finish main thread can't create t2 thread so it will become sequential execution instead of multithreading .
all of you are correct. because in a multithreading application, there comes a time where another thread requires an object or value from another thread for processing. in that case, this comes in handy.
When u call join then it simply means main thread goes to sleep state and t1.join is running unless t1 not completely execute (dead) . When first thread is done his work then join wakeup the main thread and call next thread so it means our program has only one thread when we use join method . But we want running both thread parallel. Basically due to join delay our program so we don't use it in program generally....
Its like normal function called 2 times . Imagine that is a class vinayak{ private int counter=0; void increment(){ loop 1000 times {counter++;} } main class(){ vinayak.increment(); vinayak.increment(); } then the counter will stil be 2000. To avoid this we use threads for parallel process. many threads should not use the increment function at once. so to provide accessibility to one thread at a time.
Hi navin, I have one question in this concept of threading. My question is like let us have three threads t1, t2,t3. t1 shud print 1, t2 shud print 2, t3 shud print 3 , again t1 shud print 4, t2 shud 5 , t3 shud print 6 and so on like this . class Hi extends Thread { public synchronized void run(){ for(int i=1;i
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 ?
The 2 threads that we are using can do stuff independently. They must only wait when using our synchronized method. In this example used for learning it is useless, it's true (the whole work is only +1 ~ lol). But imagine that instead of "+1" the thread had to do some other function/method. Then both threads can work at the same time and only have to wait for each other to finish, before using our synchronized method! Let's say thread 1 counts to 10 million and the other also. Both threads can count to 10 million independently, but then they have to wait before using the final method to save their work, so no data is lost. Saving their work (the threads work) can happen very fast, but their work not. So for example if we have 1 thread, you need to count to 20 million, then save. With 2 threads you can only count to 10 million twice at the same time, then save ( and add the numbers together and get 20 million). You save about half the time with 2 threads, because the waiting is very small compared to the work they do. I hope this is more clear now :)
how is join not allowing the first thread to complete and then execute the second thread, isn't that the purpose of join ? why do I feel you didn't use join property like start three 1 then join, then start thread 2 then join. or for the sake of the demo, you could have ignore join completely I believe.
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.
thanks mate
Thanks :)
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"
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
@@sidharthbisoi1492simply adding synchronized diminishes the need for thread.join. however both things do the same
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.
So if you want a method that can be used by one thread at a time
-then make that method synchronized()
@@andrejtrozic509 i was asked this question on an interview..its very imp
Very clear and simple example, thank you!
champav basu... intha simple ga telchesav, thread safe synchronization yappati nundo doubt. simply superb. hyd lo unte kaludam we can have good biryani..
Best Java Teacher in TH-cam 💯
That is a gem of an explanation, thank you Sir!
Bucky Roberts from India .
Amazing tutorials sir!
your videos are very good. perfect point-to-point explanation such as mechanical students are also getting interested.
Great video, thank you! One of the best Java instructors
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.
Crisp and clear explanation!!
great sir very clear explanation
one of the best multithreading explanation on TH-cam
YOU are most talented navin.. best and simple way to understand that how u elaborate the concepts..
Awesome and very clear explanation and easy to understand
Godlike explanation, CLEAR AND SIMPLE !
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.
What an explaination sir .Just Wow.
Sir you are Legend for CS Students.
Thank you so much for creating such a useful video series ....it's been so useful for me...
thank u so much sir . such a beautiful explanation
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).
Naveen,
You are an awesome teacher!!
Excellent video, thanks so much, you made it easy and simple to understand :)
best channel ever
awesome explanation with example
this saved so much of my time! thank you so much!
explanation was crystal clear. Thanks a ton!
You saved me so much time...
absolute best !!!!!telusko🔥🔥🔥🔥
Excellent video, makes understanding very simple and easy!!
Awesomeee.... so concise and perfect explanation
Thank you sir really it was helped me to understand this concept clearly
Great example brother
@Telusko
We need teachers like you in college : )
Ahhhh Bossss! What an explanation!
if u use
t1.start ();
t1.join ();
t2.start ();
t2.join ();
then there is no need of synchronised method
btw u r a good teacher
In that case, its not multithreading because until t1 finish main thread can't create t2 thread so it will become sequential execution instead of multithreading .
In that case its not parallel processing .
all of you are correct. because in a multithreading application, there comes a time where another thread requires an object or value from another thread for processing. in that case, this comes in handy.
Well explained and to the point thank you so much for the amazing video!
Thank you for explaining it comprehensively.
Sir, I have a doubt
when use threads like this
t1.start();
t1.join();
t2.start();
t2.join();
we get 2000 without "synchronized " keyword
When u call join then it simply means main thread goes to sleep state and t1.join is running unless t1 not completely execute (dead) .
When first thread is done his work then join wakeup the main thread and call next thread so it means our program has only one thread when we use join method . But we want running both thread parallel.
Basically due to join delay our program so we don't use it in program generally....
Its like normal function called 2 times .
Imagine that is a
class vinayak{
private int counter=0;
void increment(){
loop 1000 times {counter++;}
}
main class(){
vinayak.increment();
vinayak.increment();
}
then the counter will stil be 2000. To avoid this we use threads for parallel process.
many threads should not use the increment function at once. so to provide accessibility to one thread at a time.
wow! crystal clear explanation.THANK YOU!
An amateur is asking this :
What's the Point of thread concept then???
probably late but rest of the code in threads (before and after the method call) run simultaneously.
@5:24 join() still presented for both right then why it's not giving 2000
Brilliant explanation, thanks for sharing!
Good Explanation.. Thanks
Excellent explanation sir
Crystal clear!!!much appreciated!
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?
Its really great. kudos to navin reddy
Can you make a video about hibernate? and lambda? thank youuuu
Great explanation....!!! Thank you.
Top class teaching ..👏👏👍
What happens when you call an unsynchronized method inside a synchronized method? Will the output be consistent?
Thanks for this question.
Because of this question, i tried calling unsynchronized method inside a synchronized method.
The output is consistent.
thankyou so much for great explaination
i learnt three things
1. Synchronized keyword
2. join method
3. Thread safe class
Hi navin, I have one question in this concept of threading. My question is like let us have three threads t1, t2,t3. t1 shud print 1, t2 shud print 2, t3 shud print 3 , again t1 shud print 4, t2 shud 5 , t3 shud print 6 and so on like this .
class Hi extends Thread
{
public synchronized void run(){
for(int i=1;i
Great video, nice explanation
Thanks! great explanation on this topic!
Thank you very much for this very informative and clear video. It helped me a lot :D
A question, why you didn’t use implements or extends ?
Thank you for the amazing video
Because he didn't make new classes for custom threads, he used the classes Thread and Runnable themselves, a month late but well...
thaaaaaaanks very much & I hope you'll explain the Thread pool
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 ?
probably late but rest of the code in threads (before and after the method call) run simultaneously.
I have query if we are making this method synchronized ( thread safe)then what is the use of using thread???? Please answer this..
The 2 threads that we are using can do stuff independently. They must only wait when using our synchronized method. In this example used for learning it is useless, it's true (the whole work is only +1 ~ lol). But imagine that instead of "+1" the thread had to do some other function/method. Then both threads can work at the same time and only have to wait for each other to finish, before using our synchronized method! Let's say thread 1 counts to 10 million and the other also. Both threads can count to 10 million independently, but then they have to wait before using the final method to save their work, so no data is lost. Saving their work (the threads work) can happen very fast, but their work not.
So for example if we have 1 thread, you need to count to 20 million, then save. With 2 threads you can only count to 10 million twice at the same time, then save ( and add the numbers together and get 20 million). You save about half the time with 2 threads, because the waiting is very small compared to the work they do.
I hope this is more clear now :)
Sir can you please make a video on thread pool and thread group.
It is a very good video. It helped me a lot.
if only 1 thread is working at a time then why use thread concept??
Well explained sir.
Thanks a lot sir. You have very good explanation
nice explanation sir
thank you, all the best!
Good job sir
I can't control the laugh. When both intelligent are adding the same value! :)
what is the benefit of assigning priority to a thread??
excellent video
Damn cool explanation over this confusing topic....
Thanks! it helped me!
Awesome tutorials
Thank you so much bro!
it worked!
nicely explained good example. thanx..
Thank You. Nicely explained
good explanation. Thanks for the tutorial!
thank u soo much sir nice explanation...
The ways of your explanation is awesome...😊
How to sort list of string using multithreading in java?
can we put the count variable synchronized instead??
You are the best
I am getting output count = 2000 without using the synchronised keyword
try 10000 or even a bigger number.
is there any difference if i run the same code in java 8? I did the same code without join() and i got 2000 everytime as the count
what is the drawback of using synchronized .?
very clear . thank you sir.
Sir I getting an error on forloop at c what should I do .I am also updated jdk 7 to 8
but sir u are giving the join method in t1 and t2 so it means t1 will work separately and also t2 then why the count is coming as weird
Nice explained......
thanks sir you explain very well
Sir,why didn't you use try and catch keywords when you used throws ?
Even though the count is not initialised why he didn't get the error?
Without synchronized the condition is in deadlock?
Very helpful video
sir how we can refer to a non-final variable inside an inner class defined in a different method
Same.
Local variable c is accessed from within inner class;needs to be declared final..
Thank you so much!!! It helped alot!!
how is join not allowing the first thread to complete and then execute the second thread, isn't that the purpose of join ?
why do I feel you didn't use join property like start three 1 then join, then start thread 2 then join.
or for the sake of the demo, you could have ignore join completely I believe.