נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Deadlock, livelock ו-starvationDeadlock, livelock & starvation
מפגש 38: איך threads נתקעים, איך מאבחנים עם jstack ו-thread dump, ואיך מונעים: סדר מנעולים, tryLock וזמנים קצובים.Session 38: How threads get stuck, how to diagnose with jstack and a thread dump, and how to prevent it: lock ordering, tryLock and timeouts.
איך 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. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- נעילה בסדר הפוךLocking in opposite orders
- אותו סדר לכולםThe same order for everyone
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- 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.
- livelock: threads לא תקועים, אבל כל הזמן מוותרים זה לזה ומנסים שוב, ולכן לא מתקדמים.livelock: Threads aren't stuck, but keep giving way to each other and retrying, so they never progress.
- הרעבה: thread שכמעט אף פעם לא מקבל את המנעול כי אחרים תמיד מקדימים.starvation: A thread that almost never gets the lock because others always get there first.
- thread dump: תמונה של כל ה-threads ומה כל אחד מחזיק ומחכה לו.thread dump: A snapshot of every thread and what it holds and waits for.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
נעילה בסדר הפוךLocking in opposite orders
נעילה בסדר הפוךLocking in opposite orders
- 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).
- כאן
tryLock(200ms) מגלה שמשהו לא בסדר ומוותר.Here tryLock(200ms) notices something's wrong and backs off.
T1 נועל A ואז רוצה B; T2 נועל B ואז רוצה A. בלי זמן קצוב זה deadlock (את הגרסה הזו רק מדמים, כי היא הייתה תוקעת את הבנייה). כאן tryLock(200ms) מגלה שמשהו לא בסדר ומוותר. בסימולטור נסו לייצר את התקיעה בעצמכם.T1 locks A then wants B; T2 locks B then wants A. Without a timeout that's a deadlock (we only simulate that version, since it would hang the build). Here tryLock(200ms) notices something's wrong and backs off. In the simulator, try causing the freeze yourself.נושאTopic 3
אותו סדר לכולםThe same order for everyone
אותו סדר לכולםThe same order for everyone
- שני ה-threads נועלים קודם A ואז B.Both threads lock A first and then B.
- מעגל לא יכול להיווצר, ושניהם תמיד מצליחים.A cycle can't form, and both always succeed.
שני ה-threads נועלים קודם A ואז B. מעגל לא יכול להיווצר, ושניהם תמיד מצליחים.Both threads lock A first and then B. A cycle can't form, and both always succeed.נושאTopic 4
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves
Shelf.lend(from, to) מעבירה ספר אחד בין מדפים, עם ReentrantLock לכל מדף, תמיד לפי id (הקטן קודם). שני threads מעבירים בכיוונים הפוכים. מ-home=3 ו-club=1: home=3 club=1.Shelf.lend(from, to) moves one book between shelves, with a ReentrantLock per shelf, always by id (smaller first). Two threads move in opposite directions. From home=3 and club=1: home=3 club=1.deadlock: שני threads (או יותר) שכל אחד מחזיק משהו שהשני צריך, ומחכה לו.deadlock: Two (or more) threads each holding something the other needs, and waiting for it.
המונחים של המפגשThis session's termsסדר נעילה: אם כולם נועלים מנעולים באותו סדר (למשל לפי id), מעגל המתנה לא יכול להיווצר.lock ordering: If everyone takes locks in the same order (say by id), a waiting cycle can't form.
המונחים של המפגשThis session's termslivelock: threads לא תקועים, אבל כל הזמן מוותרים זה לזה ומנסים שוב, ולכן לא מתקדמים.livelock: Threads aren't stuck, but keep giving way to each other and retrying, so they never progress.
המונחים של המפגשThis session's termsהרעבה: thread שכמעט אף פעם לא מקבל את המנעול כי אחרים תמיד מקדימים.starvation: A thread that almost never gets the lock because others always get there first.
המונחים של המפגשThis session's termsthread dump: תמונה של כל ה-threads ומה כל אחד מחזיק ומחכה לו.thread dump: A snapshot of every thread and what it holds and waits for.
המונחים של המפגשThis session's termsT1 נועל A ואז רוצה B; T2 נועל B ואז רוצה A.T1 locks A then wants B; T2 locks B then wants A.
נעילה בסדר הפוךLocking in opposite ordersבלי זמן קצוב זה deadlock (את הגרסה הזו רק מדמים, כי היא הייתה תוקעת את הבנייה).Without a timeout that's a deadlock (we only simulate that version, since it would hang the build).
נעילה בסדר הפוךLocking in opposite ordersכאן tryLock(200ms) מגלה שמשהו לא בסדר ומוותר.Here tryLock(200ms) notices something's wrong and backs off.
נעילה בסדר הפוךLocking in opposite ordersשני ה-threads נועלים קודם A ואז B.Both threads lock A first and then B.
אותו סדר לכולםThe same order for everyoneמעגל לא יכול להיווצר, ושניהם תמיד מצליחים.A cycle can't form, and both always succeed.
אותו סדר לכולםThe same order for everyoneמדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה צריך כדי שיהיה deadlock?What's needed for a deadlock?
- מעגל: כל thread מחזיק משהו ומחכה למשהו שאחר מחזיקA cycle: each thread holds something and waits for something another holds
- שני threadsTwo threads
- הרבה זיכרוןLots of memory
- synchronized אחדOne synchronized
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מעגל: כל thread מחזיק משהו ומחכה למשהו שאחר מחזיקA cycle: each thread holds something and waits for something another holds
נכון.Right.הדרך הפשוטה ביותר למנוע deadlock בין שני מנעולים?The simplest way to prevent a deadlock between two locks?
- כולם נועלים באותו סדרEveryone locks in the same order
- להוסיף sleepAdd a sleep
- volatilevolatile
- יותר threadsMore threads
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כולם נועלים באותו סדרEveryone locks in the same order
נכון.Right.התוכנית קפאה בלי שגיאה. מה הכלי הראשון?The program froze with no error. The first tool?
- thread dump עם
jstackA thread dump with jstack - להוסיף printlnAdd a println
- להריץ שובRun again
- javacjavac
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.thread dump עם jstackA thread dump with jstack
נכון: רואים מי מחזיק מה ומחכה למה.Right: you see who holds what and waits for what.שני מדפים, סדר אחדTwo shelves, one order
move(who) נועלת את shelfA ואז את shelfB.move(who) locks shelfA then shelfB.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.העברה בנקאית בלי deadlockA bank transfer without deadlock
transfer(from, to, amount) נועלת את שני החשבונות.transfer(from, to, amount) locks both accounts.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: from ואז toBug hunter: from then to
- בגרסה הזו
transfer נועלת from ואז to.In this version transfer locks from then to.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.tryLock מוותרtryLock gives up
- כתבו את
work מהתזכורת:Write work from the refresher:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves
Shelf.lend(from, to) מעבירה ספר אחד בין מדפים, עם ReentrantLock לכל מדף, תמיד לפי id (הקטן קודם).Shelf.lend(from, to) moves one book between shelves, with a ReentrantLock per shelf, always by id (smaller first).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מנעול הוגן נגד הרעבהA fair lock against starvation
new ReentrantLock(true) משרת לפי סדר הגעה.new ReentrantLock(true) serves in order of arrival.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.לגלות deadlock מתוך התוכניתDetecting a deadlock from inside
- שני threads (daemon, כדי שהתוכנית תוכל להסתיים) נכנסים ל-deadlock אמיתי.Two threads (daemons, so the program can still end) enter a real deadlock.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שני ה-threads משתמשים באותה מתודה, ולכן באותו סדר.Both threads use the same method, so the same order.
שני מדפים, סדר אחדTwo shelves, one orderAccount first = from.id < to.id ? from : to;Account first = from.id < to.id ? from : to;
העברה בנקאית בלי deadlockA bank transfer without deadlockT1 נועל x ומחכה ל-y; T2 נועל y ומחכה ל-x.T1 locks x and waits for y; T2 locks y and waits for x.
צייד באגים: from ואז toBug hunter: from then toהתוכנית מסתיימת תמיד, גם כשהיה deadlock פוטנציאלי.The program always ends, even when a deadlock was possible.
tryLock מוותרtryLock gives upשני מנעולים מקוננים, כל אחד עם try/finally משלו.Two nested locks, each with its own try/finally.
מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelvesמנעול לא הוגן היה יכול לתת למישהו "לעקוף בתור".An unfair lock could let someone "cut the line".
מנעול הוגן נגד הרעבהA fair lock against starvationמה למדנוWhat we learned
- deadlock: שני threads (או יותר) שכל אחד מחזיק משהו שהשני צריך, ומחכה לו.deadlock: Two (or more) threads each holding something the other needs, and waiting for it.
- T1 נועל A ואז רוצה B; T2 נועל B ואז רוצה A.T1 locks A then wants B; T2 locks B then wants A.
- שני ה-threads נועלים קודם A ואז B.Both threads lock A first and then B.
- מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.