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

JavaJava
31
חלקPart 6: Concurrency ו-ThreadsConcurrency & threads כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Thread ו-Runnable, start מול run, ו-joinThread & Runnable, start vs run, and join

מה זה thread, יצירה עם Runnable ולמבדה, ההבדל בין start ל-run, join, ומצבי ה-thread.

What a thread is, creating one with Runnable and lambdas, the difference between start and run, join, and thread states.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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

1. מה ההבדל בין t.start() ל-t.run()?What's the difference between t.start() and t.run()?

2. שני threads מדפיסים A ו-B. מה מובטח?Two threads print A and B. What's guaranteed?

3. מה getState() של thread שנוצר ועוד לא הופעל?What's getState() of a thread created but not started?

תרגיליםExercises

תרגילExercise 1

שני דובריםTwo speakers

שני threads: אחד מדפיס Noa 1 עד Noa 3, השני Avi 1 עד Avi 3 (מתודה say(name)). חכו לשניהם עם join והדפיסו done. הסדר בין Noa ל-Avi יכול להשתנות, אבל done תמיד אחרון.

Two threads: one prints Noa 1 to Noa 3, the other Avi 1 to Avi 3 (a say(name) method). Wait for both with join and print done. The order between Noa and Avi can change, but done is always last.

איך הפלט אמור להיראות?What should the output look like?
Noa 1
Noa 2
Noa 3
Avi 1
Avi 2
Avi 3
done
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: new Thread(() -> say("Noa"))new Thread(() -> say("Noa"))
לפתרוןSolution
תרגילExercise 2

סכום בשני חצאיםA sum in two halves

Summer implements Runnable סוכם קטע של מערך לתוך שדה sum משלו. שני threads סוכמים את 1–50 ואת 51–100. אחרי join הדפיסו 1275 + 3775 = 5050.

Summer implements Runnable sums a slice of an array into its own sum field. Two threads sum 1–50 and 51–100. After join print 1275 + 3775 = 5050.

איך הפלט אמור להיראות?What should the output look like?
1275 + 3775 = 5050
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 כותב לשדה שלו: אין מה לסנכרן.Each thread writes to its own field: nothing to synchronize.
רמזHint 2: בלי join אפשר לקרוא את sum לפני שהחישוב נגמר.Without join you might read sum before the work is done.
לפתרוןSolution
תרגילExercise 3

צייד באגים: run במקום startBug hunter: run instead of start

התוכנית מדפיסה loading in main פעמיים: שום thread חדש לא נוצר. תקנו כך שכל טוען ירוץ ב-thread שלו (Loader-1, Loader-2), וחכו לשניהם.

The program prints loading in main twice: no new thread was created. Fix it so each loader runs on its own thread (Loader-1, Loader-2), and wait for both.

איך הפלט אמור להיראות?What should the output look like?
loading in Loader-2
loading in Loader-1
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: start() ואז join().start() then join().
לפתרוןSolution
תרגילExercise 4

צייד באגים: לא חיכינוBug hunter: we didn't wait

התוכנית מדפיסה בדרך כלל Loaded 0 books: main קורא את המונה לפני שהטוען סיים. הוסיפו את מה שחסר כך שיודפס תמיד Loaded 3 books.

The program usually prints Loaded 0 books: main reads the counter before the loader finished. Add what's missing so it always prints Loaded 3 books.

איך הפלט אמור להיראות?What should the output look like?
Loaded 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.
רמזHint 1: loader.join(); לפני ההדפסה.loader.join(); before printing.
לפתרוןSolution
תרגילExercise 5

מדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once

פרויקט מלווהCompanion project

כתבו שני קבצים (fiction.txt עם 3 ספרים, poetry.txt עם 2). Loader implements Runnable קורא קובץ לתוך רשימה משלו. הפעילו שני טוענים במקביל, חכו, והדפיסו Loaded 5 books from 2 files.

Write two files (fiction.txt with 3 books, poetry.txt with 2). Loader implements Runnable reads a file into its own list. Start two loaders at once, wait, and print Loaded 5 books from 2 files.

איך הפלט אמור להיראות?What should the output look like?
Loaded 5 books from 2 files
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: run() לא יכול לזרוק IOException: עוטפים ב-UncheckedIOException.run() can't throw IOException: wrap it in an UncheckedIOException.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מצבי threadThread states

הדפיסו את המצב של thread לפני start (NEW), ואחרי join (TERMINATED alive=false). באמצע ה-thread מדפיס working.

Print a thread's state before start (NEW), and after join (TERMINATED alive=false). In between the thread prints working.

איך הפלט אמור להיראות?What should the output look like?
NEW
working
TERMINATED alive=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: t.getState(), t.isAlive()t.getState(), t.isAlive()
לפתרוןSolution
אתגרChallenge 2

מקסימום בארבעה threadsA maximum on four threads

חלקו מערך של 8 מספרים ל-4 חלקים. כל thread מוצא את המקסימום בחלק שלו וכותב לתא משלו במערך best. אחרי join לכולם, הדפיסו [42, 19, 88, 61] -> 88.

Split an 8-number array into 4 parts. Each thread finds its part's maximum and writes to its own cell of a best array. After joining them all, print [42, 19, 88, 61] -> 88.

איך הפלט אמור להיראות?What should the output look like?
[42, 19, 88, 61] -> 88
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: למבדה יכולה להשתמש רק במשתנים effectively final: int from = p * size; int slot = p; בתוך הלולאה.A lambda can only use effectively final variables: int from = p * size; int slot = p; inside the loop.
לפתרוןSolution

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