למה count++ מאבד עדכונים, קטע קריטי, synchronized על מתודות ועל בלוקים, ומוניטורים.
Why count++ loses updates, critical sections, synchronized methods and blocks, and monitors.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
מונה בטוח, ארבעה threadsA safe counter, four threads
Counter עם synchronized על increment ועל get. ארבעה threads, כל אחד 10,000 פעמים. הדפיסו תמיד 40000.
Counter with synchronized on increment and get. Four threads, 10,000 times each. Always print 40000.
איך הפלט אמור להיראות?What should the output look like?
40000
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:גם get צריכה synchronized, כדי לראות את הערך האחרון.get needs synchronized too, to see the latest value.
שני threads מוסיפים כל אחד 1000 מספרים ל-ArrayList משותפת. ArrayList לא בטוחה ל-threads: עטפו את ה-add ב-synchronized (lock) על עצם פרטי, והדפיסו תמיד 2000.
Two threads each add 1000 numbers to a shared ArrayList. ArrayList isn't thread-safe: wrap the add in synchronized (lock) on a private object, and always print 2000.
איך הפלט אמור להיראות?What should the output look like?
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:private static final Object lock = new Object();private static final Object lock = new Object();
רמזHint 2:בלי הנעילה: פחות מ-2000, או אפילו ArrayIndexOutOfBoundsException.Without the lock: less than 2000, or even an ArrayIndexOutOfBoundsException.
צייד באגים: עדכונים אבודיםBug hunter: lost updates
התוכנית מהתזכורת מדפיסה מספר שונה בכל הרצה, ובדרך כלל פחות מ-200,000. תקנו את Counter כך שתמיד יודפס 200000.
The program from the refresher prints a different number each run, usually less than 200,000. Fix Counter so it always prints 200000.
איך הפלט אמור להיראות?What should the output look like?
200000
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.
ל-Shelf יש 3 עותקים. 10 threads קוראים ל-lend(). הבדיקה וההורדה באותה מתודה synchronized, ולכן תמיד: lent 3, refused 7, left 0.
Shelf has 3 copies. 10 threads call lend(). The check and the decrement are in the same synchronized method, so it's always: lent 3, refused 7, left 0.
איך הפלט אמור להיראות?What should the output look like?
lent 3, refused 7, left 0
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:shelf::lend הוא Runnable (method reference).shelf::lend is a Runnable (a method reference).
מדף, צעד 32: מונה השאלות בטוחShelf, step 32: a thread-safe loan counter
פרויקט מלווהCompanion project
LoanCounter סופר השאלות לפי כותר ובסך הכול. ארבעה דלפקים (threads) משאילים כל אחד 500 פעם, לסירוגין Dune ו-Emma. snapshot() מחזיר עותק. הדפיסו {Dune=1000, Emma=1000} total 2000.
LoanCounter counts loans per title and in total. Four desks (threads) lend 500 times each, alternating Dune and Emma. snapshot() returns a copy. Print {Dune=1000, Emma=1000} total 2000.
איך הפלט אמור להיראות?What should the output look like?
{Dune=1000, Emma=1000} total 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:גם snapshot synchronized, ומחזירה עותק כדי שאיש לא יקרא את המפה בזמן שמשנים אותה.snapshot is synchronized too, and returns a copy so nobody reads the map while it's being changed.
מונה static ומתודה static synchronized void visit() (נועלת את המחלקה). ארבעה threads × 10,000 קריאות. הדפיסו תמיד 40000.
A static counter and a static synchronized void visit() method (it locks the class). Four threads × 10,000 calls. Always print 40000.
איך הפלט אמור להיראות?What should the output look like?
40000
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 רגילה הייתה נועלת this, אבל כאן אין עצם.A plain synchronized method would lock this, but there's no object here.
transferIn היא synchronized וקוראת ל-deposit (גם synchronized) פעמיים. בגלל שהמנעול reentrant אין תקיעה. מ-100, שני threads מעבירים 50 ו-25 (וכל העברה עולה 1): הדפיסו 173.
transferIn is synchronized and calls deposit (also synchronized) twice. Because the lock is reentrant nothing gets stuck. From 100, two threads transfer 50 and 25 (each transfer costs 1): print 173.
איך הפלט אמור להיראות?What should the output look like?
173
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:ה-thread שכבר מחזיק את this נכנס שוב בלי לחכות.The thread already holding this enters again without waiting.