ReentrantLock and ReadWriteLock, AtomicInteger and compareAndSet, and ConcurrentHashMap.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
מונה ביקורים אטומיAn atomic visit counter
static final AtomicInteger hits. ארבעה threads, כל אחד 25,000 incrementAndGet(). הדפיסו hits 100000.
static final AtomicInteger hits. Four threads, each 25,000 incrementAndGet(). Print hits 100000.
איך הפלט אמור להיראות?What should the output look like?
hits 100000
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:אין צורך ב-synchronized.No synchronized needed.
מלאי 5000. שלושה threads מורידים כל אחד 1000 פעמים, כל הורדה בתוך lock.lock() ו-try/finally. הדפיסו stock 2000.
A stock of 5000. Three threads each decrement 1000 times, each decrement inside lock.lock() and try/finally. Print stock 2000.
איך הפלט אמור להיראות?What should the output look like?
stock 2000
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:Lock lock = new ReentrantLock();Lock lock = new ReentrantLock();
המפה היא ConcurrentHashMap, ובכל זאת הספירה יוצאת נמוכה מדי, ולא תמיד אותו דבר. החליפו את put(get + 1) ב-merge כך שתמיד יודפס {Dune=2000, Emma=2000}.
The map is a ConcurrentHashMap, and still the count comes out too low, and not always the same. Replace put(get + 1) with merge so it always prints {Dune=2000, Emma=2000}.
איך הפלט אמור להיראות?What should the output look like?
{Dune=2000, Emma=2000}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
שלושה threads מציעים ציונים. offer(score) מעדכנת את best רק אם הציון גבוה יותר, בלולאת compareAndSet (בלי מנעול). הדפיסו best 99.
Three threads offer scores. offer(score) updates best only if the score is higher, in a compareAndSet loop (no lock). Print best 99.
איך הפלט אמור להיראות?What should the output look like?
best 99
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:קוראים cur, אם הציון לא גבוה יותר יוצאים, אחרת מנסים compareAndSet(cur, score) עד שמצליחים.Read cur, leave if the score isn't higher, otherwise try compareAndSet(cur, score) until it works.
מדף, צעד 36: מטמון בטוח לפרטי ספריםShelf, step 36: a thread-safe book cache
פרויקט מלווהCompanion project
BookCache.details(title) משתמשת ב-computeIfAbsent של ConcurrentHashMap: הטעינה (שמגדילה loads) רצה פעם אחת לכל כותר, גם כששני threads מבקשים יחד. הדפיסו Dune (4 letters) | loaded 3 times.
BookCache.details(title) uses ConcurrentHashMap's computeIfAbsent: the load (which bumps loads) runs once per title, even when two threads ask together. Print Dune (4 letters) | loaded 3 times.
איך הפלט אמור להיראות?What should the output look like?
Dune (4 letters) | loaded 3 times
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:computeIfAbsent מריץ את הפונקציה פעם אחת לכל מפתח, באופן אטומי.computeIfAbsent runs the function once per key, atomically.
Catalog עם ReentrantReadWriteLock: add עם מנעול הכתיבה, size עם מנעול הקריאה. כותב מוסיף 3 ספרים וקורא קורא במקביל. הדפיסו 3 books.
Catalog with a ReentrantReadWriteLock: add with the write lock, size with the read lock. A writer adds 3 books while a reader reads. Print 3 books.
איך הפלט אמור להיראות?What should the output look like?
3 books
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
main מחזיק את מנעול המדפסת. thread אחר מנסה tryLock(100, MILLISECONDS), מוותר, ומדפיס printer busy, try later. main משחרר ומדפיס held by main: false.
main holds the printer lock. Another thread tries tryLock(100, MILLISECONDS), gives up, and prints printer busy, try later. main releases and prints held by main: false.
איך הפלט אמור להיראות?What should the output look like?
printer busy, try later
held by main: false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:tryLock לא מחכה לנצח, וזה כלי חשוב נגד deadlock (מפגש 38).tryLock doesn't wait forever, which is an important tool against deadlock (session 38).