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

חזרה למפגשBack
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.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • נעילה בסדר הפוךLocking in opposite orders
  • אותו סדר לכולםThe same order for everyone
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגש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.
נושא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.
נושא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.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 38: השאלה בין שני מדפיםShelf, step 38: lending between two shelves

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.

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.

שני ה-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

מה צריך כדי שיהיה 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

מעגל: כל thread מחזיק משהו ומחכה למשהו שאחר מחזיקA cycle: each thread holds something and waits for something another holds

הדרך הפשוטה ביותר למנוע deadlock בין שני מנעולים?The simplest way to prevent a deadlock between two locks?

  • כולם נועלים באותו סדרEveryone locks in the same order
  • להוסיף sleepAdd a sleep
  • volatilevolatile
  • יותר threadsMore threads

כולם נועלים באותו סדרEveryone locks in the same order

התוכנית קפאה בלי שגיאה. מה הכלי הראשון?The program froze with no error. The first tool?

  • thread dump עם jstackA thread dump with jstack
  • להוסיף printlnAdd a println
  • להריץ שובRun again
  • javacjavac

thread dump עם jstackA thread dump with jstack

שני מדפים, סדר אחדTwo shelves, one order

  • move(who) נועלת את shelfA ואז את shelfB.move(who) locks shelfA then shelfB.

העברה בנקאית בלי deadlockA bank transfer without deadlock

  • transfer(from, to, amount) נועלת את שני החשבונות.transfer(from, to, amount) locks both accounts.

צייד באגים: from ואז toBug hunter: from then to

  • בגרסה הזו transfer נועלת from ואז to.In this version transfer locks from then to.

tryLock מוותרtryLock gives up

  • כתבו את work מהתזכורת:Write work from the refresher:

מדף, צעד 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).

מנעול הוגן נגד הרעבהA fair lock against starvation

  • new ReentrantLock(true) משרת לפי סדר הגעה.new ReentrantLock(true) serves in order of arrival.

לגלות deadlock מתוך התוכניתDetecting a deadlock from inside

  • שני threads (daemon, כדי שהתוכנית תוכל להסתיים) נכנסים ל-deadlock אמיתי.Two threads (daemons, so the program can still end) enter a real deadlock.

שני ה-threads משתמשים באותה מתודה, ולכן באותו סדר.Both threads use the same method, so the same order.

Account first = from.id < to.id ? from : to;Account first = from.id < to.id ? from : to;

T1 נועל x ומחכה ל-y; T2 נועל y ומחכה ל-x.T1 locks x and waits for y; T2 locks y and waits for x.

התוכנית מסתיימת תמיד, גם כשהיה deadlock פוטנציאלי.The program always ends, even when a deadlock was possible.

שני מנעולים מקוננים, כל אחד עם try/finally משלו.Two nested locks, each with its own try/finally.

מנעול לא הוגן היה יכול לתת למישהו "לעקוף בתור".An unfair lock could let someone "cut the line".

מה למדנו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

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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