Thread ללוגיקה ול-render, עם סנכרוןA thread for logic and rendering, with sync
מפרידים את הלוגיקה מהציור ל-threads נפרדים, משתפים מצב בבטחה, ומשווים ל-Swing Timer.
Separating logic from drawing into their own threads, sharing state safely, and comparing with a Swing Timer.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
פרסום תמונת מצבPublishing a snapshot
record Snapshot(int tick, List<String> enemies) ומשתנה static volatile Snapshot latest. thread לוגיקה מפרסם שלוש תמונות. אחרי join הדפיסו tick 3 [bat@3, orc@6].
record Snapshot(int tick, List<String> enemies) and a static volatile Snapshot latest. A logic thread publishes three snapshots. After join print tick 3 [bat@3, orc@6].
איך הפלט אמור להיראות?What should the output look like?
tick 3 [bat@3, orc@6]
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:בכל טיק latest = new Snapshot(...): הפניה חדשה, לא שינוי של הישנה.Each tick latest = new Snapshot(...): a new reference, not a change to the old one.
צייד באגים: אויב נולד באמצע לולאהBug hunter: an enemy born mid-loop
כשה-orc "מוליד" תוך כדי המעבר על הרשימה, התוכנית קורסת. החליפו ל-CopyOnWriteArrayList (הלולאה עוברת על עותק) כך שיודפס [bat, orc, orc-baby].
When the orc "spawns" while the list is being looped over, the program crashes. Switch to a CopyOnWriteArrayList (the loop goes over a copy) so it prints [bat, orc, orc-baby].
איך הפלט אמור להיראות?What should the output look like?
[bat, orc, orc-baby]
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.
צעד של 0.02 שנייה. זמני פריימים: 0.016, 0.033, 0.050, 0.016, 0.005. צוברים, ומריצים עדכון כל עוד הצטבר מספיק. הדפיסו את מספר העדכונים המצטבר אחרי כל פריים: 0 2 4 5 6.
A 0.02-second step. Frame times: 0.016, 0.033, 0.050, 0.016, 0.005. Accumulate, and run an update while enough has built up. Print the running number of updates after each frame: 0 2 4 5 6.
איך הפלט אמור להיראות?What should the output look like?
0 2 4 5 6
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.
הלוגיקה כותבת פריים לחוצץ האחורי ואז swap() (synchronized) מחליף. אחרי 3 פריימים החוצץ הקדמי: [30, 31, 32, 33].
The logic writes a frame to the back buffer then swap() (synchronized) exchanges them. After 3 frames the front buffer: [30, 31, 32, 33].
איך הפלט אמור להיראות?What should the output look like?
[30, 31, 32, 33]
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:מחליפים הפניות, לא מעתיקים.Swap references, don't copy.
משחק 5: עולם עם הרבה אויביםGame 5: a world with many enemies
פרויקט מלווהCompanion project
1000 אויבים. בכל טיק מחלקים ל-4 חלקים, כל חלק מתעדכן במשימה משלו (בריכה של 4), ומחברים לעולם חדש. כל Enemy.step() מחזיר אויב חדש (בלי לשנות את הקיים), אז אין מה לנעול. אחרי 10 טיקים: 1000 enemies, 333 reached x>=30, enemy 5 at x=30.
1000 enemies. Each tick split into 4 parts, each part updated in its own task (a pool of 4), and joined into a new world. Every Enemy.step() returns a new enemy (without changing the old one), so there's nothing to lock. After 10 ticks: 1000 enemies, 333 reached x>=30, enemy 5 at x=30.
איך הפלט אמור להיראות?What should the output look like?
1000 enemies, 333 reached x>=30, enemy 5 at x=30
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:עצמים בלתי משתנים + עולם חדש בכל טיק = בלי מרוצים.Immutable objects + a new world each tick = no races.
כתבו את הלולאה מהתזכורת: scheduleAtFixedRate כל 10ms, 105ms, ואז shutdown. מספר הטיקים המדויק משתנה, ולכן מדפיסים רק אם הוא סביב 10: about 10 ticks: true.
Write the loop from the refresher: scheduleAtFixedRate every 10ms, for 105ms, then shutdown. The exact tick count varies, so print only whether it's around 10: about 10 ticks: true.
איך הפלט אמור להיראות?What should the output look like?
about 10 ticks: true
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 אחד מריץ את כל הטיקים, ולכן ArrayList בסדר כאן.One thread runs every tick, so an ArrayList is fine here.
שלושה threads מוסיפים כל אחד 5 נקודות 1000 פעמים ל-AtomicInteger. הדפיסו תמיד score 15000.
Three threads each add 5 points 1000 times to an AtomicInteger. Always print score 15000.
איך הפלט אמור להיראות?What should the output look like?
score 15000
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.