Thread Signaling in Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024

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

  • @RahulYadav-dd7qe
    @RahulYadav-dd7qe 8 หลายเดือนก่อน +10

    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  8 หลายเดือนก่อน +1

      Thank you very much for your kind words !! ... you can always share the playlist with your colleeagues... that would be helpful 😊

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

      @@JakobJenkov , Why this Channel doesn't have at least 500K Subs is beyond Logic, pls check ur Channel name for Shadow Bans or other types of Ban by Shady YT algorithm,
      U must take help of Paid SEO tools to analyze the Views or keywords appropriate for ur Channel>
      In My Opinion, whenever someone Searches for "Multi-threading concepts", urs must be the Top 5 results
      🙂🙏🙏

  • @wassimalsafwi3091
    @wassimalsafwi3091 6 หลายเดือนก่อน +5

    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  6 หลายเดือนก่อน

      You are very welcome!! :-) ... I have a few ideas for more videos about Java concurrency - incl. a video about the ForkJoinPool :-)

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

    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  ปีที่แล้ว +1

      Thank you very much !! :-)

  • @matthewsaucedo2471
    @matthewsaucedo2471 10 หลายเดือนก่อน +2

    Excellent series here. Start to finish exceptional explanations, made it easy to binge!

    • @JakobJenkov
      @JakobJenkov  9 หลายเดือนก่อน +1

      Thank you very much !! :-)

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

    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  ปีที่แล้ว +1

      You are welcome! I am happy that my tutorials and videos have helped you ! :-)

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

    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! :-)

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

    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  ปีที่แล้ว

      You are welcome ! ... I am just happy my tutorials are useful to people out there ! :-)

  • @garimadhanania1853
    @garimadhanania1853 5 หลายเดือนก่อน +1

    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! 💯💯

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

    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! :-)

  • @jayaprakashreddy9980
    @jayaprakashreddy9980 3 หลายเดือนก่อน +1

    This channel is gold❤❤

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

      Thank you very much! :-) :-)

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

    Well done! Very informative video especially regarding spurious wakeups.

  • @arpitjain4239
    @arpitjain4239 8 หลายเดือนก่อน +1

    Can you please make a video on Semaphores, Mutexes about differences and similarities. 🙏
    Your videos are amazing!!

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

      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 ! :-)

  • @RossZhang-gh5of
    @RossZhang-gh5of 2 หลายเดือนก่อน +1

    Very good video! Just curious does spurious wake-up still exist in Java 22?

    • @JakobJenkov
      @JakobJenkov  2 หลายเดือนก่อน +1

      I don't actually know (maybe Google knows?), but it's probably better to be safe than sorry :-)

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

    such an amazing explaination, great stuff

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

    am I correct:
    if we have completableFuture which is supposed to run have PL/SQL query(let's says it runs for 1000 seconds) and at some point somebody calls Future.get(300ms) it throw TimeoutException but nothing will changed inside the process/future body?
    as far as I understand - it will try to activate task(or schedule if it not running already) . since it's request to JDBC Driver, then thread should be suspended (awaiting for Driver response) - it will be waked up and then it just throws TimeoutException, but in fact it will continue execution of the code? or will it kill the task, cleanup resources and send cancel signal to JDBCDriver?
    I'm associating Future.get(ms) with Lock.lock(ms) -> it's just opportunity to get resource, but it doesn't impact to the process itself?

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

    @Jakob, I see that you use notify in your examples for BoundedSemaphore. Is it possible that one consumer can wake up another consumer instead of waking up the producer ? Same goes with producer too. Any reason why notifyall is not used ? I was thinking notifyall wouldnt cause the problem of consumer not waking up another consumer.

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

      Typically, consumers and producers will be calling wait() on different objects, so you know that you only awaken consumers or producers, and not all threads.

  • @RahulKumar-ed1bt
    @RahulKumar-ed1bt 2 ปีที่แล้ว +1

    Best explanation ever

  • @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.

  • @garimadhanania1853
    @garimadhanania1853 5 หลายเดือนก่อน +1

    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  5 หลายเดือนก่อน +1

      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() .

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

    Thanks for the series

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

      You are welcome! :-)

  • @garimadhanania1853
    @garimadhanania1853 5 หลายเดือนก่อน +1

    question: if say more than 1 thread called wait and then 1 called notify(), how is it decided which one to notify?

    • @JakobJenkov
      @JakobJenkov  5 หลายเดือนก่อน +1

      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.

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

    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  ปีที่แล้ว +3

      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.

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

    Perfect! Thank you.

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

    Awesome plz explain more advance concepts here

  • @MyTube.0101
    @MyTube.0101 2 ปีที่แล้ว +1

    Nice explanation!!

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

    Very helpful

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

    What is different between a Condition Variable and Any Other Object when used for signalling?

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

      What do you mean by "condition variable" ?

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

      @@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 2 ปีที่แล้ว +1

    Hello Jakob, you upload this to github???
    thank you :)

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

      I just did ! :-) ... it's now part of my Java Examples repository!
      github.com/jjenkov/java-examples

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

    thank you.

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

    Why so complex so? If one solution works, then I think pp would interest in the working solution...

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

      I teach principles and techniques - bit final answers 😉