נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
34
חלקPart 6: Concurrency ו-ThreadsConcurrency & threads כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

wait/notify, producer/consumer ו-BlockingQueuewait/notify, producer/consumer & BlockingQueue

המתנה לתנאי עם wait ו-notify, תבנית יצרן-צרכן, ו-BlockingQueue שעושה את זה בשבילכם.

Waiting for a condition with wait and notify, the producer-consumer pattern, and BlockingQueue that does it for you.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • wait ו-notify: lock.wait() משחרר את המנעול ונרדם עד ש-lock.notifyAll() מעיר אותו (ואז הוא מחכה לקבל את המנעול שוב).wait and notify: lock.wait() releases the lock and sleeps until lock.notifyAll() wakes it (then it waits to get the lock back).
  • התעוררות שקרית: thread יכול להתעורר מ-wait בלי סיבה, או אחרי שמישהו אחר כבר לקח את מה שחיכה לו.spurious wakeup: A thread can wake from wait for no reason, or after someone else already took what it waited for.
  • הצרכן נועל, ובודק ב-while אם יש חבילה.The consumer locks, and checks in a while whether there's a parcel.
  • אין?None?
  • ArrayBlockingQueue בגודל 2: היצרן נחסם כשהתור מלא, והצרכן נחסם כשהוא ריק.An ArrayBlockingQueue of size 2: the producer blocks when the queue is full, the consumer blocks when it's empty.
  • הסדר processed 1..5 מובטח, כי זה תור.The order processed 1..5 is guaranteed, because it's a queue.
  • מדף, צעד 34: תור ייבואShelf, step 34: an import queue

מונחיםTerms

  • wait ו-notifywait and notify: lock.wait() משחרר את המנעול ונרדם עד ש-lock.notifyAll() מעיר אותו (ואז הוא מחכה לקבל את המנעול שוב). שניהם רק בתוך synchronized (lock).lock.wait() releases the lock and sleeps until lock.notifyAll() wakes it (then it waits to get the lock back). Both only inside synchronized (lock).
  • התעוררות שקריתspurious wakeup: thread יכול להתעורר מ-wait בלי סיבה, או אחרי שמישהו אחר כבר לקח את מה שחיכה לו. לכן בודקים את התנאי ב-while, לא ב-if.A thread can wake from wait for no reason, or after someone else already took what it waited for. That's why the condition is checked in a while, not an if.
  • יצרן-צרכןproducer-consumer: thread אחד (או יותר) מייצר עבודה ושם בתור, ואחרים לוקחים ומעבדים. התור מאזן בין המהירויות.One thread (or more) produces work into a queue, and others take and process it. The queue balances the speeds.
  • BlockingQueueBlockingQueue: תור בטוח ל-threads: put מחכה כשהוא מלא, take מחכה כשהוא ריק. ArrayBlockingQueue (גודל קבוע), LinkedBlockingQueue.A thread-safe queue: put waits when it's full, take waits when it's empty. ArrayBlockingQueue (fixed size), LinkedBlockingQueue.
  • גלולת רעלpoison pill: ערך מיוחד שהיצרן שם בתור כדי לומר לצרכן "זהו, אפשר לסיים". אחד לכל צרכן.A special value the producer puts in the queue to tell a consumer "that's it, you can stop". One per consumer.
  • wait ו-notifywait and notify: lock.wait() משחרר את המנעול ונרדם עד ש-lock.notifyAll() מעיר אותו (ואז הוא מחכה לקבל את המנעול שוב). שניהם רק בתוך synchronized (lock).lock.wait() releases the lock and sleeps until lock.notifyAll() wakes it (then it waits to get the lock back). Both only inside synchronized (lock).
  • התעוררות שקריתspurious wakeup: thread יכול להתעורר מ-wait בלי סיבה, או אחרי שמישהו אחר כבר לקח את מה שחיכה לו. לכן בודקים את התנאי ב-while, לא ב-if.A thread can wake from wait for no reason, or after someone else already took what it waited for. That's why the condition is checked in a while, not an if.
  • יצרן-צרכןproducer-consumer: thread אחד (או יותר) מייצר עבודה ושם בתור, ואחרים לוקחים ומעבדים. התור מאזן בין המהירויות.One thread (or more) produces work into a queue, and others take and process it. The queue balances the speeds.
  • BlockingQueueBlockingQueue: תור בטוח ל-threads: put מחכה כשהוא מלא, take מחכה כשהוא ריק. ArrayBlockingQueue (גודל קבוע), LinkedBlockingQueue.A thread-safe queue: put waits when it's full, take waits when it's empty. ArrayBlockingQueue (fixed size), LinkedBlockingQueue.
  • גלולת רעלpoison pill: ערך מיוחד שהיצרן שם בתור כדי לומר לצרכן "זהו, אפשר לסיים". אחד לכל צרכן.A special value the producer puts in the queue to tell a consumer "that's it, you can stop". One per consumer.

Cheatsheet

wait ו-notifyAllwait and notifyAll
Thread consumer = new Thread(() -> {
    synchronized (lock) {
        while (parcel == null) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                return;
            }
        }
        System.out.println("got " + parcel);
BlockingQueue: אותו דבר, בלי waitBlockingQueue: the same, without wait
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2);
Thread producer = new Thread(() -> {
    try {
        for (int i = 1; i <= 5; i++) {
            queue.put(i);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
});

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

נבנה על ידיBuilt by Ariel Yohimovich