Thread Signaling in Java

  Рет қаралды 10,593

Jakob Jenkov

Jakob Jenkov

Күн бұрын

Java has a set of thread signaling features that enable one Java thread to coordinate its actions with another. These basic thread signaling features are provided via the Object class wait() , notify() and notifyAll() methods.
When implementing thread signaling in Java you need to be aware of the problem of missed signals and spurious wakeups. I cover both in this video too.
Chapters:
0:00 Java Thread signaling introduction - wait(), notify() and notifyAll()
1:50 Calling wait(), notify() or notifyAll() must be done from within a synchronized block
2:31 Calling wait() releases the synchronization lock
5:50 Designing a thread signaling class in Java
9:17 Missed signals
17:24 notify() vs. notifyAll()
19:24 Spurious wakeups
Java Thread Signaling - text:
jenkov.com/tutorials/java-con...
Java Concurrency - text:
jenkov.com/tutorials/java-con...
Java Concurrency - playlist:
• Java Concurrency and M...

Пікірлер: 53
@RahulYadav-dd7qe
@RahulYadav-dd7qe 5 ай бұрын
This is easily the BEST EVERY PLAYLIST of any tutorial I've ever seen. Trust me when I say this, I've watched ~500-1K tutorial videos over youtube of various kinds, but the satisfaction I got from this playlist is beyond my words. I would've easily paid $100 equivalent in my currency for this.
@JakobJenkov
@JakobJenkov 4 ай бұрын
Thank you very much for your kind words !! ... you can always share the playlist with your colleeagues... that would be helpful 😊
@wassimalsafwi3091
@wassimalsafwi3091 3 ай бұрын
Outstanding series, Jakob. It was clear and concise, with many examples, yet rich in theory. Thank you very much for your work. I am looking forward to your next video about multithreading, hoping you will include ForkJoin, CompletableFuture, and virtual threads.
@JakobJenkov
@JakobJenkov 3 ай бұрын
You are very welcome!! :-) ... I have a few ideas for more videos about Java concurrency - incl. a video about the ForkJoinPool :-)
@kimo8154
@kimo8154 Жыл бұрын
thank you verry much. believe me i read/watched every single tutorial you made on your website/youtube channel. you are a legend and thank you again, again and again
@JakobJenkov
@JakobJenkov Жыл бұрын
Thank you very much !! :-)
@garimadhanania1853
@garimadhanania1853 2 ай бұрын
This is the best ever playlist!! I really enjoyed learning these concepts and its a must watch for those using Java concurrency!! Thanks for your efforts Jakob! 💯💯
@vijaysekharvn
@vijaysekharvn 10 ай бұрын
thank you for the wonderful cource on threads. i have been looking ofr this kind of information on thread as Thread is scariest topic to face in the interview
@JakobJenkov
@JakobJenkov 10 ай бұрын
You are welcome ! ... I am just happy my tutorials are useful to people out there ! :-)
@matthewsaucedo2471
@matthewsaucedo2471 6 ай бұрын
Excellent series here. Start to finish exceptional explanations, made it easy to binge!
@JakobJenkov
@JakobJenkov 6 ай бұрын
Thank you very much !! :-)
@alexv3312
@alexv3312 Жыл бұрын
Jakob, this entire series is great. I'll gladly recommend it to anyone as an alternative of companion to the Concurrency in Practice book. Well done!
@JakobJenkov
@JakobJenkov Жыл бұрын
Thank you very much! :-)
@damercy
@damercy Жыл бұрын
I read your tutorials on your website 2 years ago and learned collections from there. Really grateful for such clear, concise and deep explanations. I'm happy to see the video versions of concurrency etc are being published as well. Thank you for these. I'm now a native android developer who just did his bachelors in CSE last year and thanks to you, I am somewhat confident in these concepts (so much to learn yet!). Thank you Jakob. :) I hope to see more videos on this channel as well.
@JakobJenkov
@JakobJenkov Жыл бұрын
You are welcome! I am happy that my tutorials and videos have helped you ! :-)
@edihoxhalli4885
@edihoxhalli4885 Жыл бұрын
Well done! Very informative video especially regarding spurious wakeups.
@JakobJenkov
@JakobJenkov Жыл бұрын
Thanks Edi! :-)
@jeandy4495
@jeandy4495 Жыл бұрын
Thank you for your videos. I'm studying concurrent systems concepts and your website as well as your videos are a perfect complement.
@JakobJenkov
@JakobJenkov Жыл бұрын
Great! I am happy to hear that! :-)
@tahiriqbal8543
@tahiriqbal8543 11 ай бұрын
such an amazing explaination, great stuff
@JakobJenkov
@JakobJenkov 10 ай бұрын
Thank you !!
@arpitjain4239
@arpitjain4239 4 ай бұрын
Can you please make a video on Semaphores, Mutexes about differences and similarities. 🙏 Your videos are amazing!!
@JakobJenkov
@JakobJenkov 4 ай бұрын
Hmm... I have a long list of video topics, and not so much time, unfortunately ! ... so I am not sure if I can get around to it! ... but who knows! Maybe ! :-)
@RahulKumar-ed1bt
@RahulKumar-ed1bt Жыл бұрын
Best explanation ever
@JakobJenkov
@JakobJenkov Жыл бұрын
Thanks :-D
@wujasmarecki
@wujasmarecki Жыл бұрын
Perfect! Thank you.
@JakobJenkov
@JakobJenkov Жыл бұрын
You are welcome! :-)
@adithyabhat4770
@adithyabhat4770 6 ай бұрын
Thanks for the series
@JakobJenkov
@JakobJenkov 6 ай бұрын
You are welcome! :-)
@manoj.tyg01
@manoj.tyg01 Жыл бұрын
Nice explanation!!
@JakobJenkov
@JakobJenkov Жыл бұрын
Thank you! :-)
@Theonadj
@Theonadj Жыл бұрын
Awesome plz explain more advance concepts here
@Hrishikeshdarshan
@Hrishikeshdarshan Жыл бұрын
Very helpful
@JakobJenkov
@JakobJenkov Жыл бұрын
Thanks :-)
@leejeam851
@leejeam851 Жыл бұрын
thank you.
@JakobJenkov
@JakobJenkov Жыл бұрын
You are welcome :-)
@garimadhanania1853
@garimadhanania1853 2 ай бұрын
could you please share some usecases where a thread would need to wait on an object? The only vague thing I can think of is that when a thread is waiting for some data that is probably going to be written to the signalObject by the notifier..kind of like future
@JakobJenkov
@JakobJenkov 2 ай бұрын
wait() is used e.g. when a thread is waiting to take some elements from a BlockingQueue that is currently empty. Then the thread waits until another thread puts some elements into the queue and calls notify() .
@polacofederos
@polacofederos Жыл бұрын
nice tutorials!!! just wondering, it would be "beneficial" that flag variables be "volatile" ?
@JakobJenkov
@JakobJenkov Жыл бұрын
Only if accessed by more than one thrread.
@rm6267
@rm6267 Жыл бұрын
In this case 12:47 it's not necessary to used atomic type in signalRaised because is synchronized all the class with synchronized(this) {}?
@JakobJenkov
@JakobJenkov Жыл бұрын
Inside a synchronized block you do not need to use atomic types, nor volatile variables. However, then all access to these variables (fields) should be within synchronized blocks.
@garimadhanania1853
@garimadhanania1853 2 ай бұрын
question: if say more than 1 thread called wait and then 1 called notify(), how is it decided which one to notify?
@JakobJenkov
@JakobJenkov 2 ай бұрын
There is no guarantee about which waitinf thread gets notified with a notify() call. This can result in "Thread Starvation" . The solution is to implement "fairness" among the waiting threads.
@arpitjain4239
@arpitjain4239 4 ай бұрын
What is different between a Condition Variable and Any Other Object when used for signalling?
@JakobJenkov
@JakobJenkov 4 ай бұрын
What do you mean by "condition variable" ?
@arpitjain4239
@arpitjain4239 4 ай бұрын
@@JakobJenkov I mean't using Condition Interface in Locks.
@AMITTIWARI-re9sg
@AMITTIWARI-re9sg Жыл бұрын
Great video, But I have one issue with below code - Where when my Female thread called first my male thread is keep on waiting. I don't want a signal to be missed nor I want my thread to keep on waiting. import java.util.ArrayList; import java.util.List; /* Objective is to use two threads for stundent list and each thread will responsible for doing the needful based on Gender In below example I have just used to print the name of male and fémale student with the help of different thread. Another important aspect is both thread are communicating with standard wait and notify mechanism and the lock is on same signal object */ public class MultiThread { static List studentList = new ArrayList(); private boolean isThreadWaiting = false; private boolean signalRaised = false; public static void main(String[] args) throws InterruptedException { Student s1= new Student("Amit", "male"); Student s2= new Student("Amita", "female"); Student s3= new Student("Chandan", "male"); Student s4= new Student("Amrita", "female"); Student s5= new Student("Ajay", "male"); Student s6= new Student("Chandana", "female"); studentList.add(s1); studentList.add(s2); studentList.add(s3); studentList.add(s4); studentList.add(s5); studentList.add(s6); Object signalObject = new Object(); MultiThread multiThread = new MultiThread(); Runnable runnable=() -> { synchronized (signalObject) { System.out.println("Calling " + Thread.currentThread()); try { if (multiThread.signalRaised) { multiThread.signalRaised=false; return; } System.out.println(Thread.currentThread().getName() + " going to wait"); multiThread.isThreadWaiting=true; signalObject.wait(); multiThread.isThreadWaiting=false; System.out.println(Thread.currentThread().getName() + " wait ended"); } catch (InterruptedException e) { e.printStackTrace(); } studentList.stream().sorted().filter(student -> student.getGender().equals("male")).forEach(student -> System.out.println(student)); } }; Runnable runnable2=() -> { synchronized (signalObject) { System.out.println("Calling "+Thread.currentThread()); studentList.stream().sorted().filter(student -> student.getGender().equals("female")).forEach(student -> System.out.println(student)); if(!multiThread.isThreadWaiting) { multiThread.signalRaised=true; } signalObject.notify(); System.out.println(Thread.currentThread().getName()+" After notify existing synchronized block"); } }; startThreads(runnable, runnable2); } private static void startThreads(Runnable runnable, Runnable runnable2) { Thread t1= new Thread(runnable, "Male Thread 1"); Thread t2= new Thread(runnable2, "Female Thread 1"); t2.start(); t1.start(); } }
@ivib570
@ivib570 Жыл бұрын
Hello Jakob, you upload this to github??? thank you :)
@JakobJenkov
@JakobJenkov Жыл бұрын
I just did ! :-) ... it's now part of my Java Examples repository! github.com/jjenkov/java-examples
@bruceliebewilma
@bruceliebewilma Жыл бұрын
Why so complex so? If one solution works, then I think pp would interest in the working solution...
@JakobJenkov
@JakobJenkov Жыл бұрын
I teach principles and techniques - bit final answers 😉
Java ForkJoinPool
27:32
Jakob Jenkov
Рет қаралды 3,1 М.
Java Threads - Creating, starting and stopping threads in Java
17:14
I’m just a kid 🥹🥰 LeoNata family #shorts
00:12
LeoNata Family
Рет қаралды 20 МЛН
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 113 МЛН
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 22 МЛН
Compare and Swap in Java
24:21
Jakob Jenkov
Рет қаралды 15 М.
Producer Consumer Pattern - With Java Example
19:50
Jakob Jenkov
Рет қаралды 26 М.
how to export svg from illustrator
1:20
Latino Web Studio
Рет қаралды 2,8 М.
Thread Pools in Java
18:04
Jakob Jenkov
Рет қаралды 63 М.
Java Virtual Threads
9:06
Jakob Jenkov
Рет қаралды 33 М.
13.7 Multithreading Synchronized Keyword
10:17
Telusko
Рет қаралды 431 М.
Java BlockingQueue
17:48
Jakob Jenkov
Рет қаралды 38 М.
The Java Memory Model - The Basics
23:41
Jakob Jenkov
Рет қаралды 120 М.
Java Happens Before Guarantee - Java Memory Model - Part 2
26:13
Jakob Jenkov
Рет қаралды 55 М.
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 3,9 МЛН
Simple maintenance. #leddisplay #ledscreen #ledwall #ledmodule #ledinstallation
0:19
LED Screen Factory-EagerLED
Рет қаралды 16 МЛН
Ультрабюджетная игровая мышь? 💀
1:00