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

חזרה למפגשBack
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.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • שני threads, סדר לא ידועTwo threads, an unknown order
  • start מול runstart vs run
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגש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.
נושא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.
נושא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.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

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

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.

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.

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.

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

מה ההבדל בין 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 parallel
  • start מחכה לסיוםstart waits for the end

start יוצר thread חדש, run רץ ב-thread הנוכחיstart makes a new thread, run runs on the current one

שני 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

שניהם יודפסו, בסדר כלשהוBoth get printed, in some order

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

  • NEWNEW
  • RUNNABLERUNNABLE
  • WAITINGWAITING
  • TERMINATEDTERMINATED

NEWNEW

שני דובריםTwo speakers

  • שני threads:Two threads:

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

  • Summer implements Runnable סוכם קטע של מערך לתוך שדה sum משלו.Summer implements Runnable sums a slice of an array into its own sum field.

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

  • התוכנית מדפיסה loading in main פעמיים:The program prints loading in main twice:

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

  • התוכנית מדפיסה בדרך כלל Loaded 0 books:The program usually prints Loaded 0 books:

מדף, צעד 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).

מצבי threadThread states

  • הדפיסו את המצב של thread לפני start (NEW), ואחרי join (TERMINATED alive=false).Print a thread's state before start (NEW), and after join (TERMINATED alive=false).

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

  • חלקו מערך של 8 מספרים ל-4 חלקים.Split an 8-number array into 4 parts.

new Thread(() -> say("Noa"))new Thread(() -> say("Noa"))

כל thread כותב לשדה שלו: אין מה לסנכרן.Each thread writes to its own field: nothing to synchronize.

start() ואז join().start() then join().

loader.join(); לפני ההדפסה.loader.join(); before printing.

מה למדנו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

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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