9:00 Stopping threads Synchronized keyword will stop threads executing the method concurrently but if a new 2nd object is created, then two different threads can call the method concurrently on each of the object instances. So synchronized only works if multiple threads call same object 8:30 the native way of stopping a thread will stop a thread in an unkown state. Implement a runnable that has a stop function instead of using the default stop method. The custom stop function sets a stop requested variable, then the runnable run method can keep checking if a stop has been requested or not and terminate on its own terms (saving any state before stopping). 12:00 the main thread can terminate, but as long as any of the threads it started are still running, then the JVM will stay running. Daemon threads will NOT keep the jvm alive if they are the last thread running, so if you want the jvm to stop running after all other threads have terminated thread.setDaemon(true) 14:10 can make main thread wait for another thread by using thread.join 15:15 OS threads vs more lightweight threads project loom (fibers)
@luckyluke24834 жыл бұрын
hey Jakob. I usually give a visit to your webiste at work on a weekly basis. just wanted to say thanks for the effort. It has helped me a lot at work
@JakobJenkov4 жыл бұрын
You are welcome :-) Happy my tutorials help people out there :-)
@JanChodura68 Жыл бұрын
Still great! Thanks. One small thing. I added print to main method after calling run of thread and now voiala you can see main thread is finished but thread still runs and shows its print
@mortbringsli3 жыл бұрын
I have to thank you for the fantastic work you do here. This video (along with the related article) both summarized and was more informative than the last three lectures on multithreading in my comp-sci course. People like you make learning easy, and are a real gift to the world.
@JakobJenkov3 жыл бұрын
Thank you very much! - and I am happy that my tutorials have helped you learn!
@Eduardado3 жыл бұрын
Jakob, this was an amazing explanation. Thanks a lot :D. Subscribed!
@JakobJenkov3 жыл бұрын
Great! And thanks! :-)
@JonasKeil2 жыл бұрын
Another awesome video, thanks Jakob!👏
@JakobJenkov2 жыл бұрын
You are welcome! :-)
@hailiu22954 жыл бұрын
Thanks for your effort. I also like watching your textual tutorials. As a fresh Java Programmer, I acquired more new knowledge from your tutorial. Thanks a lot!
@JakobJenkov4 жыл бұрын
Thanks! - Great to hear :-)
@destro59903 жыл бұрын
Loved it, threads have always been hard to grasp but you made it way easier
@JakobJenkov3 жыл бұрын
Great ! 😊
@enable44 жыл бұрын
I just don't have words to express all my gratitude for all your work
@JakobJenkov4 жыл бұрын
Thank you! :-) The words you did have are still very much appreciated :-)
@sebastiantomczyk4577Ай бұрын
Does the textual version include all the informations that KZbin course have(I mean for entire JAVA THREADS, not just for this episode)? I prefer textual version more but I dont want to miss any bit of what YT course gives. Thanks for the answer!
@JakobJenkovАй бұрын
Usually the articles and videos have the same amount of details 😊
@sebastiantomczyk4577Ай бұрын
@@JakobJenkov thank you 👍🏻
@arisdideambemou15083 ай бұрын
Thanks for your video. It was amazing a bring a lot a knowledge !
@JakobJenkov3 ай бұрын
Thanks!! Glad you liked it !! 😊
@sonalgupta70832 жыл бұрын
When and how the runnable stops the thread after checking the stopRequested variable?
@JakobJenkov2 жыл бұрын
Then the Runnable.run() method exits (returns) - meaning it simply finishes executing - then the Thread executing the Runnable stops automatically.
@someshpandey4282 жыл бұрын
Fantastic Playlist!! Hope you'll continue making these awesome videos!! Your website and content there is superb as well !! One thing, Is there a git repo for the code snippets etc. used in this playlist? It will be much helpful.
@gaomingt3 жыл бұрын
When using lambda expression to create a new thread, isn't it more readable to implement Runnable interface like the following code snippet? Thread thread = new Thread ( () -> { System.out.println("Lambda running"); System.out.println("Lambda finished"); } ); thread.start(); Anyways, thanks for making this awesome multithreading series. High quality content!
@JakobJenkov3 жыл бұрын
That is a question of personal style preference. Your way can work just fine too :-)
@burakb87083 жыл бұрын
Thanks for your explanation.
@JakobJenkov3 жыл бұрын
You are welcome
@singhvertika81674 жыл бұрын
Your videos are just outstanding..I feel more confident in java concurrency now..thanks
@JakobJenkov4 жыл бұрын
Thank you very much !! I have more videos on Java concurrency coming soon !!
@supercrazysquid4 жыл бұрын
Thank you! Amazing tutorial and easy to follow!
@JakobJenkov4 жыл бұрын
Thank you - glad it was helpful! :-)
@mouradlaraba3 жыл бұрын
thanks a lot sir for your generosity, i learn a lot from your courses, big hug for you :)
@JakobJenkov3 жыл бұрын
Glad to be able to help :-)
@BillPark-ey6ih Жыл бұрын
I appreciate your tutorials. They are really helpful as I had no clue during the lecture haha. Also, your videos are very well organized and concise unlike many other videos. Thank you!
@JakobJenkov Жыл бұрын
You are welcome! ... and thank you for taking the time to tell me! :-)
@GeoffClark3 жыл бұрын
THANK YOU so much. This solved a "freezing" issue when I was trying to use media in my JSwing. So helpful!
@JakobJenkov3 жыл бұрын
Great ;-)
@alihosseinkhani26712 ай бұрын
thank you. there course is helping insane!
@JakobJenkov2 ай бұрын
Thank you 😊😊
@MrDe03 жыл бұрын
very straightforward !! amazing
@JakobJenkov3 жыл бұрын
I try my best to make the content easy to understand :-)
@PriyasKitchen213 жыл бұрын
how to stop freezing ui using multithreading using java
@JakobJenkov3 жыл бұрын
You would normally delegate the work you have to do to a background thread, rather than executing it directly in the UI thread. When the background thread finishes its work, the last thing it does it to send a message back to the UI thread with the result. I have described how you do that in JavaFX (see link below). I will be similar in other UI toolkits such as Java Swing, Java AWT or SWT. tutorials.jenkov.com/javafx/concurrency.html
@St.Pa-al8 ай бұрын
Good stuff, just stumbled upon you Jakob.
@JakobJenkov8 ай бұрын
Thank you ! :-) Glad you liked my videos :-)
@swagatpatra21393 жыл бұрын
Thanks, nice video.
@JakobJenkov3 жыл бұрын
You are welcome! Glad you liked it! :-)
@Das.Kleine.Krokodil Жыл бұрын
Great videos, thanks! Are there source codes?
@JakobJenkov Жыл бұрын
There are some, here: github.com/jjenkov/java-examples
@kaka_tutorails87656 күн бұрын
Thank man I learn something from you.
@SouthernSeneca18 күн бұрын
This playlist is a jackpot for learning concurrency in Java, thanks for creating these
@JakobJenkov17 күн бұрын
Great ! Thanks :-)
@nathansmalley84334 жыл бұрын
Great instruction!
@JakobJenkov4 жыл бұрын
Thank you very much !!!
@shubhamagarwal14344 ай бұрын
God of Concurency..i have bene flowing you since my 2014 when you used to write blog post only...by going through your post i attend interview like a LION when they ask mutithreading qns....Thanks a Lot form 10+ yrs exp guy from BLR,India.
@JakobJenkov4 ай бұрын
Thanks! :-)
@smellthel3 жыл бұрын
thanccc
@JakobJenkov3 жыл бұрын
You are welcome :-)
@justvibhor374310 ай бұрын
Thanks a lot i was struggling with that !!
@JakobJenkov9 ай бұрын
You are welcome! :-)
@WiseFatOwl4 жыл бұрын
Hello Jakob.. Loved this simple and straight to the point tutorial. Thank you for making this!
@JakobJenkov4 жыл бұрын
Cool - you are welcome :-)
@aaavalenca3 жыл бұрын
12:18 "And keeps the Java Virtual Machine ~alive~", says he, as if he was a mad scientist. Jokes aside, wonderful videos! It's been of great help :))
@JakobJenkov3 жыл бұрын
;-)
@waqasahmed86604 ай бұрын
Thank you for this amazing tutorial that explains a lot of important concepts related to Threading in Java. Much appreciated.
@JakobJenkov4 ай бұрын
You are welcome !! 😊 Happy it was helpful to you !! 😊
@SoeaOu3 жыл бұрын
thanks, this series is much better then reading the boring "java concurrency" book short and to the point examples you can follow along, 10/10
@JakobJenkov3 жыл бұрын
Thanks :-D ... I hope my voice doesn't make you fall asleep :-D
@heclosty-wk3bj Жыл бұрын
Thanks a lot, but do you have GitHub source, so we can practice with you
@JakobJenkov Жыл бұрын
The Java examples I have in GitHub are found in this repo: github.com/jjenkov/java-examples
@heclosty-wk3bj Жыл бұрын
@@JakobJenkov cool, but it doesn't have this video's code, so I have uploaded in your repo
@matthewsaucedo24719 ай бұрын
@@heclosty-wk3bj @JakobJenkov Can you merge his PR?
@josephkwagilwa84492 жыл бұрын
Great Content for Java Concurrency
@JakobJenkov2 жыл бұрын
Thanks, Joseph :-)
@subbunittala20123 жыл бұрын
this is just awesome , Jenkov .. i usually go through your website ..thank you very much for sharing knowelege at diamond level !
@JakobJenkov3 жыл бұрын
Hi Subbu! Thank you for your kind words! I am happy that my articles and videos are helpful for you! :-)
@_graymatter__4 жыл бұрын
Good to have a revision on this concept. Thanks for you effort.
@JakobJenkov4 жыл бұрын
Ah, so you noticed that this video replaces an older video I've made on this topic?? ... which had text flickering, and covered less topics etc. ;-)
@رياحالهاشمية Жыл бұрын
From Saudi Arabia thx🇸🇦🇸🇦🇸🇦 u help me to understand well my lessons
@JakobJenkov11 ай бұрын
You are welcome :-)
@lifesport13802 жыл бұрын
Thank you very much❤️
@JakobJenkov2 жыл бұрын
You're welcome 😊
@chirag28198 ай бұрын
Thank you for saving me from those dry technical books.
@JakobJenkov8 ай бұрын
You are welcome ! ;-)
@andgoleczka3 жыл бұрын
Very nice educational videos. I like how you explain things, understandable language and powerful examples! Well done Jakob!
@JakobJenkov3 жыл бұрын
Thank you very much! :-)
@WhiteBerry3 жыл бұрын
Hi Jakob, thanks for your clear explanations - while I still can't get my head around this I believe I may need to take things a little slower in order to fully understand your code examples. Nevertheless, thank you for your explanations and keep it up! :)
@JakobJenkov3 жыл бұрын
Thanks ! ... what exactly can't your get your head around?
@peshutanpavri1599 Жыл бұрын
Fantastic playlist and great info, thank you so much, your other website has so much amazing information. I only have one question about best practices and organising code that will be multi-threaded. Most of the time, I would implement a runnable interface, or use a lambda/method reference. If I have some long running task simulating processing lots of data etc.. Should the core logic be inside the run method or should the run method be in a separate class that only deals with the logic of handling threads. Because I may a split a task between multiple threads ? For example, If I get some data from a database or an api, then I have a huge list of data that i want to process, say I have a method called processData, should that logic be inside run or should i call processData from run ? Just wanted to know if there are any best practices to organising this. Also server side multi-threading seems to be different ? Because there is no main UI thread or UI rendering server side, I guess it would just be large computations ? Making a request to a DB could be on the main thread itself ? Especially as it sequential ? In order to respond to a request, I would need to wait for database to respond anyway, I am so sorry i just had one question about organising multithreaded code but went on a tangent, Your videos and online tutorials are so helpful Thank you
@JakobJenkov Жыл бұрын
Sorry about the late reply! ... I would keep the run() method as small as possible, and move the application logic to separate classes / objects. On the server - it depends on the threading model used by the web server you are using. If the web server uses one thread per connection, you can block that thread and call a database or remote HTTP / REST API from inside that thread. However, if the web server uses a single-threaded design (as Netty or Undertow does), then you should NOT call a database or remote HTTP / REST API from that thread, but instead call that from a background thread, so you don't block the single thread serving requests from the users.
@peshutanpavri1599 Жыл бұрын
@@JakobJenkov Thank you, not to worry, I appreciate you taking the time to get back to me. Is there anyway you have some more detailed info / video about how NIO networking works ? Im really struggling to understand the fundamentals and how to get started, I want to build my own web server Thanks
@willemsmit29294 жыл бұрын
Much Appreciated! Helped a lot.
@JakobJenkov4 жыл бұрын
Great - glad it helped! :-)
@asdenakisolmonkate9467 Жыл бұрын
It is amazing continue
@JakobJenkov Жыл бұрын
Thank you 😊
@KratosProton Жыл бұрын
Great explanation!
@JakobJenkov Жыл бұрын
Glad it was helpful ! :-)
@journeytothedream61273 жыл бұрын
Lots of love from the bottom of my heart
@JakobJenkov3 жыл бұрын
You are welcome! I am glad my tutorials help people out there! :-)
@gregorytippett96923 жыл бұрын
Exceptional tutorial. Very clearly explained & well-organized examples!
@JakobJenkov3 жыл бұрын
Thank you! :-)
@MohitKhare Жыл бұрын
thank you Jakob
@JakobJenkov Жыл бұрын
You are welcome! :-)
@paulisaichuk5034 Жыл бұрын
Excellent video, great job, thanks!
@JakobJenkov Жыл бұрын
Thanks - glad you liked it! :-)
@chingiskhant49713 жыл бұрын
This is great!
@JakobJenkov3 жыл бұрын
Thanks! :-)
@HariKrishnaMajety3332 жыл бұрын
Hey Jakob! Thanks a ton for these Amazing lectures! Your explanations are very clear. Btw, do you plan on making videos about Java Futures?
@JakobJenkov2 жыл бұрын
Thanks ! Yes, at some point I will get to Java Future too !
@HariKrishnaMajety3332 жыл бұрын
Thanks Jakob! Videos on CompletableFutures in Java would be great!
@whereispie3 жыл бұрын
Thanks 👍❤️
@JakobJenkov3 жыл бұрын
You're welcome 😊
@SkiesTribune Жыл бұрын
Adding the synchronized keyword on both flag control methods ensures immediate stopping of threads that share the same runnable (no more printing)(t2,t1,t1,t2,t2,stop) and removing them will allow an extra sout to be printed even after stopping (t2,t1,t1,stop,t1,t2) If you synchronize the runnable sleep() method, this will ensure each thread prints a statement in an orderly fashion (t1, t2, t1, t2, t1,stop). If you synchronize the run() method itself, then one thread will be printing statements as it's not gonna leave the shared resource at all(t1,t1,t1,t1...stop).
@RoadRoller013 жыл бұрын
great video
@JakobJenkov3 жыл бұрын
Thank you very much! :-)
@MohammedMutwakil3 жыл бұрын
for 3 days I'm looking for a person who can explain what is this thread and how I can use it, finally I understand it, you are really amazing how you make it very simple, keep going and never stop, thank you very much for your effort.
@JakobJenkov3 жыл бұрын
I am glad my video helped you! :-) ... nothing more frustrating than browsing / googling around for answers to a problem that you know lots of other people have the answer to, but you just can't find it. Believe me - I have tried that a lot during my 20+ years as a Java developer :-D
@MohammedMutwakil3 жыл бұрын
Hi @@JakobJenkov, I have question please, the Thread in Android has something called Handler, is this Handler is used in Java? if it's, and you cover it please show me in which video.
@ahsanmuhammad74532 жыл бұрын
Thank you for these well explained videos :)
@JakobJenkov2 жыл бұрын
You're very welcome! :-)
@minasalah82103 жыл бұрын
thank's
@JakobJenkov3 жыл бұрын
You are welcome :-)
@anthonysouza-pl5pm7 ай бұрын
bro randomly went to another level idk what is happening
@JakobJenkov7 ай бұрын
Whar do you mean ? 😊
@Hubertoom3 жыл бұрын
So obvious, so clear. Great job!
@JakobJenkov3 жыл бұрын
Thanks - glad it was easy to understand! :-)
@Hubertoom3 жыл бұрын
@@JakobJenkov I'm learning from Hyperskill by JetBrains and often when some topic is hard or not enough elaborated or sometimes vague described peoples putting references to your and baeldung sites :) It's almost always makes things clear, you have gift to explain hard topics. As Aristotle said "Teaching is the highest form of understanding." You got it :) Thanks a lot for your job!
@JakobJenkov3 жыл бұрын
You are welcome !! Glad my tutorials are helpful to people out there ! :-)
@nelisiwemkhatshwa16893 жыл бұрын
creating an accesses functions for the inserting and the updating functions that can be callable outside the jar
@JakobJenkov3 жыл бұрын
I don't think I understand what you mean?
@Golipillas3 жыл бұрын
Thanks Jakob, incredible teacher.
@JakobJenkov3 жыл бұрын
You are welcome :-)
@RagingAcid3 жыл бұрын
This is a fantastic video. The chapters are such a nice touch
@JakobJenkov3 жыл бұрын
Thanks !! 😊😊
@vojindjukic27194 жыл бұрын
Very good, thank you sir
@JakobJenkov4 жыл бұрын
You are very welcome :-)
@aveatdj3 жыл бұрын
great explication thank you
@JakobJenkov3 жыл бұрын
You are welcome!
@diogosilva82193 жыл бұрын
this really helps thx a lot keep it up!
@JakobJenkov3 жыл бұрын
Great! ... will try to publish more videos in the future, but I don't have as much time at the moment as I used to... but I am recording a video right now, so I will release a new video soon!
@blackcitadel372 жыл бұрын
14:15 Isn't it using .join( ) on a daemon thread a bad idea?
@JakobJenkov2 жыл бұрын
Well, if the thread calling join() is kept alive (not terminating) as long as the daemon thread is running - what could happen? ("what could possibly go wrong?" :-D )
@kurisuta1305 Жыл бұрын
Amazing! I have an interview for a job that specializes threads, and your video is HEAVEN sent :D
@JakobJenkov Жыл бұрын
Great timing :-)
@eduardojavierlatorrevandep45832 жыл бұрын
Such a good tutorial!
@JakobJenkov2 жыл бұрын
Great to hear it is helpful for you! :-)
@davidchen68692 жыл бұрын
Hey Jakob, massive thanks for the help! I do, however, wonder how come the ... are still printed after the printing of "stop requested". How would one be able to prevent this from happening?
@alessandro-belottidigital71612 жыл бұрын
if you'd like to prevent this behaviour you'd basically need some sort of shared boolean variable between the two threads and do stuff according to it
@matus66813 жыл бұрын
Great video! Is there and easy way how to explain difference in Java vs Python threading module? As Python thanks to GIL is single threaded what does it mean in comparison to the Java? In other words what is possible to achieve with java that is not possible with python in terms of multithreading? Thanks!
@JakobJenkov3 жыл бұрын
Hi Matus, I don't know Python well enough at this point to say what the difference between Java threads and Python threads are, unfortunately.
@MarceloAX9 ай бұрын
The clarity, conciseness and simplicity of your videos and articles are a immense contribution for tech!! Thank you so much for your effort!!
@JakobJenkov9 ай бұрын
Thank you very much for your kind words! :-)
@sebon113 жыл бұрын
Greeeeat explanation!
@JakobJenkov3 жыл бұрын
Thanks :-)
@xavierdupont2926 Жыл бұрын
Are there pros and cons in using the same runnable instance to instanciate multiple threads?
@JakobJenkov Жыл бұрын
If your Runnable instance has any member variables, those would be shared among the threads running that instance. Most often you do not want to do that. You want each thread to have its own Runnable instance (at least that is the most common use I have seen).
@xavierdupont2926 Жыл бұрын
@@JakobJenkov Thanks, I understood that better when I watched your video on the Java memory model.
@vivekpanday2 жыл бұрын
Awesome as usual 👍
@JakobJenkov2 жыл бұрын
Thanks! :-)
@LyashenkoSergiy4 жыл бұрын
Very informative!
@JakobJenkov4 жыл бұрын
Thanks ! :-)
@kmdsummon3 жыл бұрын
Why do you create stoppable runnable to stop if the same effect can be done with interrupt, but interrupt is better in all ways?
@JakobJenkov3 жыл бұрын
calling interrupt() on a thread only works if A) The thread is blocked somewhere e.g. in a wait() call which respects the interrupt signal. B) The thread does itself check its own interrupt signal. While I could have used the interrupted flag on a thread to signal it to stop, I prefer making the signals more explicit. This way you know that the stop signal does not mean it should immediately abort everything it does *in the middle of its loop*, but can do so *at the boundaries (beginning) of its loop*. There is a slight difference there, although I understand it can be hard to see it until you know its there :-)
@maxresdefault82354 жыл бұрын
GREAT TUTORIAL
@JakobJenkov4 жыл бұрын
Thank you very much! :-)
@yog29154 жыл бұрын
Good stuff thanks to your blogs my understanding of concurrency is clear
@JakobJenkov4 жыл бұрын
Great - that makes me happy to hear ! :-) I have more videos on Java Concurrency coming !
@zhaoyinqin37283 жыл бұрын
Can you provide an example where 2 threads calls requestStop() or/and isStopRequested() at the same time? I can't think of an example since it is the runnable which calls the method, not the thread, which leads to another question, why do we need the synchronized keyword here?
@JakobJenkov3 жыл бұрын
if the stopRequested variable is a member variable of the Runnable implementation, then yes, the threads will share that member variable. Thus, when one thread is requested to stop, the other thread will stop too. Normally you do not pass the same Runnable implementation to different threads, because you often have some control logic inside them that you want to be controllable for each thread. Rather, if the threads are to share objects, these objects are accessed from inside the Runnable implementations.
@zhaoyinqin37283 жыл бұрын
@@JakobJenkov Thank you
@jefferycroom75363 жыл бұрын
I am linking this KZbin page to my class in a discussion about Violent Deaths and threads and not using the variable for Java thread. This is a great example of some of the information we are covering. Great Job on explaining.
@JakobJenkov3 жыл бұрын
I am not sure I understand exactly what your class is about?
@jefferycroom75363 жыл бұрын
@@JakobJenkov we are going over Creating threads and we were going over Violent stops using just a stop method() and why not yo use it. We are also going over starting Java Threads
@azizbekibnhamid642 Жыл бұрын
Thanks
@JakobJenkov Жыл бұрын
Welcome :-)
@juandebenedetti28842 жыл бұрын
Jakob, I am Latino and I have to tell you that you are the best and most detailed in explaining. I am super grateful to be able to learn from your videos and website. Graciass!!
@JakobJenkov2 жыл бұрын
Thank you very much! :-) I am happy that my videos are helpful for you! :-)