חלקPart 6: Concurrency ו-ThreadsConcurrency & threads
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
Deadlock, livelock ו-starvationDeadlock, livelock & starvation
איך threads נתקעים, איך מאבחנים עם jstack ו-thread dump, ואיך מונעים: סדר מנעולים, tryLock וזמנים קצובים.
How threads get stuck, how to diagnose with jstack and a thread dump, and how to prevent it: lock ordering, tryLock and timeouts.
הנקודות החשובותKey points
- deadlock: שני threads (או יותר) שכל אחד מחזיק משהו שהשני צריך, ומחכה לו.deadlock: Two (or more) threads each holding something the other needs, and waiting for it.
- סדר נעילה: אם כולם נועלים מנעולים באותו סדר (למשל לפי id), מעגל המתנה לא יכול להיווצר.lock ordering: If everyone takes locks in the same order (say by id), a waiting cycle can't form.
- T1 נועל A ואז רוצה B; T2 נועל B ואז רוצה A.T1 locks A then wants B; T2 locks B then wants A.
- בלי זמן קצוב זה deadlock (את הגרסה הזו רק מדמים, כי היא הייתה תוקעת את הבנייה).Without a timeout that's a deadlock (we only simulate that version, since it would hang the build).
- שני ה-threads נועלים קודם A ואז B.Both threads lock A first and then B.
- מעגל לא יכול להיווצר, ושניהם תמיד מצליחים.A cycle can't form, and both always succeed.
- מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves
מונחיםTerms
- deadlockdeadlock: שני threads (או יותר) שכל אחד מחזיק משהו שהשני צריך, ומחכה לו. אף אחד לא יתקדם לעולם, והתוכנית "קופאת" בלי שגיאה.Two (or more) threads each holding something the other needs, and waiting for it. Nobody will ever move, and the program "freezes" with no error.
- סדר נעילהlock ordering: אם כולם נועלים מנעולים באותו סדר (למשל לפי id), מעגל המתנה לא יכול להיווצר.If everyone takes locks in the same order (say by id), a waiting cycle can't form.
- livelocklivelock: threads לא תקועים, אבל כל הזמן מוותרים זה לזה ומנסים שוב, ולכן לא מתקדמים. פתרון: המתנה אקראית לפני ניסיון חוזר.Threads aren't stuck, but keep giving way to each other and retrying, so they never progress. Fix: a random wait before retrying.
- הרעבהstarvation: thread שכמעט אף פעם לא מקבל את המנעול כי אחרים תמיד מקדימים. מנעול הוגן (
new ReentrantLock(true)) משרת לפי סדר הגעה.A thread that almost never gets the lock because others always get there first. A fair lock (new ReentrantLock(true)) serves in order of arrival. - thread dumpthread dump: תמונה של כל ה-threads ומה כל אחד מחזיק ומחכה לו.
jstack <pid>(או Ctrl+Break) מדפיס אותה, וכותבFound one Java-level deadlockכשיש.A snapshot of every thread and what it holds and waits for.jstack <pid>(or Ctrl+Break) prints it, and saysFound one Java-level deadlockwhen there is one. - deadlockdeadlock: שני threads (או יותר) שכל אחד מחזיק משהו שהשני צריך, ומחכה לו. אף אחד לא יתקדם לעולם, והתוכנית "קופאת" בלי שגיאה.Two (or more) threads each holding something the other needs, and waiting for it. Nobody will ever move, and the program "freezes" with no error.
- סדר נעילהlock ordering: אם כולם נועלים מנעולים באותו סדר (למשל לפי id), מעגל המתנה לא יכול להיווצר.If everyone takes locks in the same order (say by id), a waiting cycle can't form.
- livelocklivelock: threads לא תקועים, אבל כל הזמן מוותרים זה לזה ומנסים שוב, ולכן לא מתקדמים. פתרון: המתנה אקראית לפני ניסיון חוזר.Threads aren't stuck, but keep giving way to each other and retrying, so they never progress. Fix: a random wait before retrying.
- הרעבהstarvation: thread שכמעט אף פעם לא מקבל את המנעול כי אחרים תמיד מקדימים. מנעול הוגן (
new ReentrantLock(true)) משרת לפי סדר הגעה.A thread that almost never gets the lock because others always get there first. A fair lock (new ReentrantLock(true)) serves in order of arrival. - thread dumpthread dump: תמונה של כל ה-threads ומה כל אחד מחזיק ומחכה לו.
jstack <pid>(או Ctrl+Break) מדפיס אותה, וכותבFound one Java-level deadlockכשיש.A snapshot of every thread and what it holds and waits for.jstack <pid>(or Ctrl+Break) prints it, and saysFound one Java-level deadlockwhen there is one.
Cheatsheet
נעילה בסדר הפוךLocking in opposite orders
Thread t1 = new Thread(() -> work("T1", a, b));
Thread t2 = new Thread(() -> work("T2", b, a));
t1.start();
t2.start();
t1.join();
t2.join();אותו סדר לכולםThe same order for everyone
Thread t1 = new Thread(() -> work("T1", a, b));
Thread t2 = new Thread(() -> work("T2", a, b));
t1.start();
t2.start();
t1.join();
t2.join();שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|