חלק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.
הנקודות החשובותKey points
- thread: "קו ביצוע" בתוך תוכנית.thread: A "line of execution" inside a program.
- Runnable: functional interface עם
void run():Runnable: A functional interface withvoid run(): start()מפעיל כל thread, ו-join()מחכה לו.start()launches each thread, andjoin()waits for it.- השורה
main is doneתמיד אחרונה, אבל A ו-B יכולים להתחלף.main is doneis always last, but A and B can swap. t.run()רץ ב-mainעצמו, כמו כל קריאה למתודה.t.run()runs onmainitself, like any method call.- רק
t.start()יוצר thread חדש בשםworker.Onlyt.start()creates a new thread namedworker. - מדף, צעד 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.mainis a thread itself. - RunnableRunnable: functional interface עם
void run(): העבודה שה-thread יעשה.new Thread(() -> ...)מקבל אותה כלמבדה.A functional interface withvoid 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 runsrun(). Callingrun()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.mainis a thread itself. - RunnableRunnable: functional interface עם
void run(): העבודה שה-thread יעשה.new Thread(() -> ...)מקבל אותה כלמבדה.A functional interface withvoid 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 runsrun(). Callingrun()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 |
|---|