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

חזרה למפגשBack
JavaJava

CompletableFuture ו-virtual threadsCompletableFuture & virtual threads

מפגש 37: שרשראות אסינכרוניות עם CompletableFuture, טיפול בשגיאות, ו-virtual threads של Java 21 לאלפי משימות.Session 37: Asynchronous chains with CompletableFuture, error handling, and Java 21 virtual threads for thousands of tasks.

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

  • מונחיםTerms
  • שרשרת של שלביםA chain of stages
  • שילוב ושגיאותCombining and errors
  • 10,000 virtual threads10,000 virtual threads
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • CompletableFuture: Future שאפשר לשרשר:CompletableFuture: A Future you can chain:
  • virtual thread: thread קל מאוד (Java 21) שה-JVM מנהל.virtual thread: A very light thread (Java 21) managed by the JVM.
  • אסינכרוני: "תתחיל את זה, ותודיע לי כשיש תוצאה", במקום לחכות בשורה.asynchronous: "Start this, and tell me when there's a result", instead of waiting in line.
נושאTopic 2

שרשרת של שלביםA chain of stages

שרשרת של שלביםA chain of stages

  • supplyAsync מריץ את fetchTitle ב-thread אחר.supplyAsync runs fetchTitle on another thread.
  • כל thenApply רץ כשהשלב הקודם מסתיים.Each thenApply runs when the previous stage finishes.
  • join() מחכה לסוף השרשרת.join() waits for the end of the chain.
נושאTopic 3

שילוב ושגיאותCombining and errors

שילוב ושגיאותCombining and errors

  • thenCombine מחכה לשתי משימות ומחבר את התוצאות.thenCombine waits for two tasks and combines the results.
  • כשמשימה נכשלת, exceptionally נותן ערך חלופי במקום לזרוק.When a task fails, exceptionally gives a fallback value instead of throwing.
נושאTopic 4

10,000 virtual threads10,000 virtual threads

10,000 virtual threads10,000 virtual threads

  • כל משימה ישנה 10ms.Each task sleeps 10ms.
  • עם 10,000 threads רגילים המחשב היה נאבק; עם virtual threads זה לוקח רגע.With 10,000 regular threads the computer would struggle; with virtual threads it takes a moment.
  • ה-try סוגר את ה-executor ומחכה לכל המשימות.The try closes the executor and waits for every task.
נושאTopic 5

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • `java.util.concurrent.CompletionException:`java.util.concurrent.CompletionException:
נושאTopic 6

פרויקט מדףShelf project

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

  • מדף, צעד 37: ייבוא עם virtual threadsShelf, step 37: import with virtual threads

CompletableFuture: Future שאפשר לשרשר:CompletableFuture: A Future you can chain:

virtual thread: thread קל מאוד (Java 21) שה-JVM מנהל.virtual thread: A very light thread (Java 21) managed by the JVM.

אסינכרוני: "תתחיל את זה, ותודיע לי כשיש תוצאה", במקום לחכות בשורה.asynchronous: "Start this, and tell me when there's a result", instead of waiting in line.

supplyAsync מריץ את fetchTitle ב-thread אחר.supplyAsync runs fetchTitle on another thread.

כל thenApply רץ כשהשלב הקודם מסתיים.Each thenApply runs when the previous stage finishes.

join() מחכה לסוף השרשרת.join() waits for the end of the chain.

thenCombine מחכה לשתי משימות ומחבר את התוצאות.thenCombine waits for two tasks and combines the results.

כשמשימה נכשלת, exceptionally נותן ערך חלופי במקום לזרוק.When a task fails, exceptionally gives a fallback value instead of throwing.

כל משימה ישנה 10ms.Each task sleeps 10ms.

עם 10,000 threads רגילים המחשב היה נאבק; עם virtual threads זה לוקח רגע.With 10,000 regular threads the computer would struggle; with virtual threads it takes a moment.

ה-try סוגר את ה-executor ומחכה לכל המשימות.The try closes the executor and waits for every task.

`java.util.concurrent.CompletionException:`java.util.concurrent.CompletionException:

מדף, צעד 37: ייבוא עם virtual threadsShelf, step 37: import with virtual threads

מה ההבדל בין thenApply ל-thenCompose?What's the difference between thenApply and thenCompose?

  • thenCompose משרשר פונקציה שמחזירה בעצמה CompletableFuturethenCompose chains a function that itself returns a CompletableFuture
  • אין הבדלNone
  • thenApply אסינכרוני ו-thenCompose לאthenApply is async and thenCompose isn't
  • thenCompose מטפל בשגיאותthenCompose handles errors

thenCompose משרשר פונקציה שמחזירה בעצמה CompletableFuturethenCompose chains a function that itself returns a CompletableFuture

מתי virtual threads עוזרים במיוחד?When do virtual threads help most?

  • כשהמשימות מחכות הרבה (רשת, קבצים, sleep)When tasks wait a lot (network, files, sleep)
  • בחישובים כבדים על המעבדIn heavy CPU calculations
  • אף פעםNever
  • רק ב-SwingOnly in Swing

כשהמשימות מחכות הרבה (רשת, קבצים, sleep)When tasks wait a lot (network, files, sleep)

משימה ב-supplyAsync זרקה חריגה. מה exceptionally(e -> 0) נותן?A task in supplyAsync threw. What does exceptionally(e -> 0) give?

  • 0 במקום החריגה0 instead of the exception
  • את החריגהThe exception
  • nullnull
  • מריץ שובRuns again

0 במקום החריגה0 instead of the exception

שרשרת של ניקויA cleaning chain

  • מתחילים עם " ulysses " ב-supplyAsync, ואז שלושה thenApply:Start with " ulysses " in supplyAsync, then three thenApplys:

שני מקורות, תוצאה אחתTwo sources, one result

  • שתי משימות במקביל:Two tasks in parallel:

צייד באגים: שגיאה בשרשרתBug hunter: an error in the chain

  • join() זורק כי ההמרה נכשלה.join() throws because the conversion failed.

allOfallOf

  • לכל כותר משימה שמחשבת עמודים (אורך × 100).A task per title computing pages (length × 100).

מדף, צעד 37: ייבוא עם virtual threadsShelf, step 37: import with virtual threads

  • 1000 שורות, כל ייבוא "מחכה לרשת" 20ms.1000 lines, each import "waits for the network" 20ms.

thenComposethenCompose

  • findId(title) ו-loadAuthor(id) מחזירות שתיהן CompletableFuture.findId(title) and loadAuthor(id) both return a CompletableFuture.

וירטואלי מול רגילVirtual vs platform

  • צרו thread וירטואלי עם Thread.ofVirtual().start(...) ו-thread רגיל עם Thread.ofPlatform().start(...).Create a virtual thread with Thread.ofVirtual().start(...) and a platform thread with Thread.ofPlatform().start(...).

מה למדנוWhat we learned

  • CompletableFuture: Future שאפשר לשרשר:CompletableFuture: A Future you can chain:
  • supplyAsync מריץ את fetchTitle ב-thread אחר.supplyAsync runs fetchTitle on another thread.
  • thenCombine מחכה לשתי משימות ומחבר את התוצאות.thenCombine waits for two tasks and combines the results.
  • כל משימה ישנה 10ms.Each task sleeps 10ms.
  • `java.util.concurrent.CompletionException:`java.util.concurrent.CompletionException:
  • מדף, צעד 37: ייבוא עם virtual threadsShelf, step 37: import with virtual threads

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

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

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