חלק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.
הנקודות החשובותKey points
- wait ו-notify:
lock.wait()משחרר את המנעול ונרדם עד ש-lock.notifyAll()מעיר אותו (ואז הוא מחכה לקבל את המנעול שוב).wait and notify:lock.wait()releases the lock and sleeps untillock.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 awhilewhether there's a parcel. - אין?None?
ArrayBlockingQueueבגודל 2: היצרן נחסם כשהתור מלא, והצרכן נחסם כשהוא ריק.AnArrayBlockingQueueof size 2: the producer blocks when the queue is full, the consumer blocks when it's empty.- הסדר
processed 1..5מובטח, כי זה תור.The orderprocessed 1..5is 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 untillock.notifyAll()wakes it (then it waits to get the lock back). Both only insidesynchronized (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 awhile, not anif. - יצרן-צרכן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:putwaits when it's full,takewaits 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 untillock.notifyAll()wakes it (then it waits to get the lock back). Both only insidesynchronized (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 awhile, not anif. - יצרן-צרכן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:putwaits when it's full,takewaits 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 |
|---|