נבנה על ידי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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • thread: "קו ביצוע" בתוך תוכנית.thread: A "line of execution" inside a program.
  • Runnable: functional interface עם void run():Runnable: A functional interface with void run():
  • 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.
  • 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.
  • מדף, צעד 31: טעינת שני קבצים במקבילShelf, step 31: loading two files at once

מונחיםTerms

  • threadthread: "קו ביצוע" בתוך תוכנית. לכל thread יש stack משלו, אבל כולם חולקים את ה-heap. main הוא thread בעצמו.A "line of execution" inside a program. Each thread has its own stack, but they all share the heap. main is a thread itself.
  • RunnableRunnable: functional interface עם void run(): העבודה שה-thread יעשה. new Thread(() -> ...) מקבל אותה כלמבדה.A functional interface with void run(): the work the thread will do. new Thread(() -> ...) takes it as a lambda.
  • start מול runstart vs run: start() יוצר thread חדש שמריץ את run(). קריאה ישירה ל-run() היא סתם קריאה למתודה, באותו thread.start() creates a new thread that runs run(). Calling run() directly is just a method call, on the same thread.
  • joinjoin: t.join() עוצר את ה-thread הנוכחי עד ש-t מסיים. זה גם מבטיח שכל מה ש-t כתב נראה אחרי ה-join.t.join() stops the current thread until t finishes. It also guarantees that whatever t wrote is visible after the join.
  • מצבי threadthread states: NEW (נוצר), RUNNABLE (רץ או מוכן), BLOCKED/WAITING/TIMED_WAITING (מחכה), TERMINATED (סיים). getState() מחזיר אותו.NEW (created), RUNNABLE (running or ready), BLOCKED/WAITING/TIMED_WAITING (waiting), TERMINATED (done). getState() returns it.
  • מתזמןscheduler: החלק במערכת ההפעלה שמחליט איזה thread רץ עכשיו ולכמה זמן. ההחלטות שלו לא צפויות, ולכן הסדר בין threads משתנה בין הרצות.The part of the OS deciding which thread runs now and for how long. Its decisions are unpredictable, so the order between threads changes between runs.
  • threadthread: "קו ביצוע" בתוך תוכנית. לכל thread יש stack משלו, אבל כולם חולקים את ה-heap. main הוא thread בעצמו.A "line of execution" inside a program. Each thread has its own stack, but they all share the heap. main is a thread itself.
  • RunnableRunnable: functional interface עם void run(): העבודה שה-thread יעשה. new Thread(() -> ...) מקבל אותה כלמבדה.A functional interface with void run(): the work the thread will do. new Thread(() -> ...) takes it as a lambda.
  • start מול runstart vs run: start() יוצר thread חדש שמריץ את run(). קריאה ישירה ל-run() היא סתם קריאה למתודה, באותו thread.start() creates a new thread that runs run(). Calling run() directly is just a method call, on the same thread.
  • joinjoin: t.join() עוצר את ה-thread הנוכחי עד ש-t מסיים. זה גם מבטיח שכל מה ש-t כתב נראה אחרי ה-join.t.join() stops the current thread until t finishes. It also guarantees that whatever t wrote is visible after the join.
  • מצבי threadthread states: NEW (נוצר), RUNNABLE (רץ או מוכן), BLOCKED/WAITING/TIMED_WAITING (מחכה), TERMINATED (סיים). getState() מחזיר אותו.NEW (created), RUNNABLE (running or ready), BLOCKED/WAITING/TIMED_WAITING (waiting), TERMINATED (done). getState() returns it.
  • מתזמןscheduler: החלק במערכת ההפעלה שמחליט איזה thread רץ עכשיו ולכמה זמן. ההחלטות שלו לא צפויות, ולכן הסדר בין threads משתנה בין הרצות.The part of the OS deciding which thread runs now and for how long. Its decisions are unpredictable, so the order between threads changes between runs.

Cheatsheet

שני threads, סדר לא ידועTwo threads, an unknown order
Thread a = new Thread(() -> System.out.println("A says hi"));
Thread b = new Thread(() -> System.out.println("B says hi"));
a.start();
b.start();
a.join();
b.join();
System.out.println("main is done");
start מול runstart vs run
Runnable job = () -> System.out.println("running in " + Thread.currentThread().getName());
Thread t = new Thread(job, "worker");
t.run();
t.start();
t.join();

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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