נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Thread ו-Runnable, start מול run, ו-joinThread & Runnable, start vs run, and join
מפגש 31: מה זה thread, יצירה עם Runnable ולמבדה, ההבדל בין start ל-run, join, ומצבי ה-thread.Session 31: What a thread is, creating one with Runnable and lambdas, the difference between start and run, join, and thread states.
מה זה 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. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- שני threads, סדר לא ידועTwo threads, an unknown order
- start מול runstart vs run
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- thread: "קו ביצוע" בתוך תוכנית.thread: A "line of execution" inside a program.
- Runnable: functional interface עם
void run():Runnable: A functional interface with void run(): - start מול run:
start() יוצר thread חדש שמריץ את run().start vs run: start() creates a new thread that runs run(). - join:
t.join() עוצר את ה-thread הנוכחי עד ש-t מסיים.join: t.join() stops the current thread until t finishes. - מצבי thread:
NEW (נוצר), RUNNABLE (רץ או מוכן), BLOCKED/WAITING/TIMED_WAITING (מחכה), TERMINATED (סיים).thread states: NEW (created), RUNNABLE (running or ready), BLOCKED/WAITING/TIMED_WAITING (waiting), TERMINATED (done). - מתזמן: החלק במערכת ההפעלה שמחליט איזה thread רץ עכשיו ולכמה זמן.scheduler: The part of the OS deciding which thread runs now and for how long.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
שני threads, סדר לא ידועTwo threads, an unknown order
שני threads, סדר לא ידועTwo threads, an unknown order
start() מפעיל כל thread, ו-join() מחכה לו.start() launches each thread, and join() waits for it.- השורה
main is done תמיד אחרונה, אבל A ו-B יכולים להתחלף.main is done is always last, but A and B can swap. - הרצנו את התוכנית באמת 20 פעמים בזמן הבנייה: הטבלה מראה מה יצא.We really ran the program 20 times while building the site: the table shows what came out.
start() מפעיל כל thread, ו-join() מחכה לו. השורה main is done תמיד אחרונה, אבל A ו-B יכולים להתחלף. הרצנו את התוכנית באמת 20 פעמים בזמן הבנייה: הטבלה מראה מה יצא. בסימולטור אתם המתזמן.start() launches each thread, and join() waits for it. main is done is always last, but A and B can swap. We really ran the program 20 times while building the site: the table shows what came out. In the simulator you're the scheduler.נושאTopic 3
start מול runstart vs run
start מול runstart vs run
t.run() רץ ב-main עצמו, כמו כל קריאה למתודה.t.run() runs on main itself, like any method call.- רק
t.start() יוצר thread חדש בשם worker.Only t.start() creates a new thread named worker. - הפלט כאן קבוע, כי
join מחכה.The output here is fixed, because join waits.
t.run() רץ ב-main עצמו, כמו כל קריאה למתודה. רק t.start() יוצר thread חדש בשם worker. הפלט כאן קבוע, כי join מחכה.t.run() runs on main itself, like any method call. Only t.start() creates a new thread named worker. The output here is fixed, because join waits.נושאTopic 4
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once
כתבו שני קבצים (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.thread: "קו ביצוע" בתוך תוכנית.thread: A "line of execution" inside a program.
המונחים של המפגשThis session's termsRunnable: functional interface עם void run():Runnable: A functional interface with void run():
המונחים של המפגשThis session's termsstart מול run: start() יוצר thread חדש שמריץ את run().start vs run: start() creates a new thread that runs run().
המונחים של המפגשThis session's termsjoin: t.join() עוצר את ה-thread הנוכחי עד ש-t מסיים.join: t.join() stops the current thread until t finishes.
המונחים של המפגשThis session's termsמצבי thread: NEW (נוצר), RUNNABLE (רץ או מוכן), BLOCKED/WAITING/TIMED_WAITING (מחכה), TERMINATED (סיים).thread states: NEW (created), RUNNABLE (running or ready), BLOCKED/WAITING/TIMED_WAITING (waiting), TERMINATED (done).
המונחים של המפגשThis session's termsמתזמן: החלק במערכת ההפעלה שמחליט איזה thread רץ עכשיו ולכמה זמן.scheduler: The part of the OS deciding which thread runs now and for how long.
המונחים של המפגשThis session's termsstart() מפעיל כל thread, ו-join() מחכה לו.start() launches each thread, and join() waits for it.
שני threads, סדר לא ידועTwo threads, an unknown orderהשורה main is done תמיד אחרונה, אבל A ו-B יכולים להתחלף.main is done is always last, but A and B can swap.
שני threads, סדר לא ידועTwo threads, an unknown orderהרצנו את התוכנית באמת 20 פעמים בזמן הבנייה: הטבלה מראה מה יצא.We really ran the program 20 times while building the site: the table shows what came out.
שני threads, סדר לא ידועTwo threads, an unknown ordert.run() רץ ב-main עצמו, כמו כל קריאה למתודה.t.run() runs on main itself, like any method call.
start מול runstart vs runרק t.start() יוצר thread חדש בשם worker.Only t.start() creates a new thread named worker.
start מול runstart vs runהפלט כאן קבוע, כי join מחכה.The output here is fixed, because join waits.
start מול runstart vs runמדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה ההבדל בין t.start() ל-t.run()?What's the difference between t.start() and t.run()?
start יוצר thread חדש, run רץ ב-thread הנוכחיstart makes a new thread, run runs on the current one- אין הבדלNone
run מהיר יותר כי הוא מקביליrun is faster because it's parallelstart מחכה לסיוםstart waits for the end
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.start יוצר thread חדש, run רץ ב-thread הנוכחיstart makes a new thread, run runs on the current one
נכון.Right.שני threads מדפיסים A ו-B. מה מובטח?Two threads print A and B. What's guaranteed?
- A לפני BA before B
- שניהם יודפסו, בסדר כלשהוBoth get printed, in some order
- B לפני AB before A
- רק אחד יודפסOnly one prints
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.שניהם יודפסו, בסדר כלשהוBoth get printed, in some order
נכון, אם מחכים להם (join).Right, if you wait for them (join).מה getState() של thread שנוצר ועוד לא הופעל?What's getState() of a thread created but not started?
NEWNEWRUNNABLERUNNABLEWAITINGWAITINGTERMINATEDTERMINATED
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.שני דובריםTwo speakers
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.סכום בשני חצאיםA sum in two halves
Summer implements Runnable סוכם קטע של מערך לתוך שדה sum משלו.Summer implements Runnable sums a slice of an array into its own sum field.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: run במקום startBug hunter: run instead of start
- התוכנית מדפיסה
loading in main פעמיים:The program prints loading in main twice:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: לא חיכינוBug hunter: we didn't wait
- התוכנית מדפיסה בדרך כלל
Loaded 0 books:The program usually prints Loaded 0 books:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once
- כתבו שני קבצים (
fiction.txt עם 3 ספרים, poetry.txt עם 2).Write two files (fiction.txt with 3 books, poetry.txt with 2).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מצבי threadThread states
- הדפיסו את המצב של thread לפני
start (NEW), ואחרי join (TERMINATED alive=false).Print a thread's state before start (NEW), and after join (TERMINATED alive=false).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מקסימום בארבעה threadsA maximum on four threads
- חלקו מערך של 8 מספרים ל-4 חלקים.Split an 8-number array into 4 parts.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.new Thread(() -> say("Noa"))new Thread(() -> say("Noa"))
שני דובריםTwo speakersכל thread כותב לשדה שלו: אין מה לסנכרן.Each thread writes to its own field: nothing to synchronize.
סכום בשני חצאיםA sum in two halvesstart() ואז join().start() then join().
צייד באגים: run במקום startBug hunter: run instead of startloader.join(); לפני ההדפסה.loader.join(); before printing.
צייד באגים: לא חיכינוBug hunter: we didn't waitמה למדנוWhat we learned
- thread: "קו ביצוע" בתוך תוכנית.thread: A "line of execution" inside a program.
start() מפעיל כל thread, ו-join() מחכה לו.start() launches each thread, and join() waits for it.t.run() רץ ב-main עצמו, כמו כל קריאה למתודה.t.run() runs on main itself, like any method call.- מדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.