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

JavaJava
37
חלקPart 6: Concurrency ו-ThreadsConcurrency & threads כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

CompletableFuture ו-virtual threadsCompletableFuture & virtual threads

שרשראות אסינכרוניות עם CompletableFuture, טיפול בשגיאות, ו-virtual threads של Java 21 לאלפי משימות.

Asynchronous chains with CompletableFuture, error handling, and Java 21 virtual threads for thousands of tasks.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

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

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

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

תרגיליםExercises

תרגילExercise 1

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

מתחילים עם " ulysses " ב-supplyAsync, ואז שלושה thenApply: trim, אות ראשונה גדולה, והוספת האורך. הדפיסו Ulysses (7).

Start with " ulysses " in supplyAsync, then three thenApplys: trim, a capital first letter, and adding the length. Print Ulysses (7).

איך הפלט אמור להיראות?What should the output look like?
Ulysses (7)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: .thenApply(String::trim).thenApply(String::trim)
לפתרוןSolution
תרגילExercise 2

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

שתי משימות במקביל: מספר עמודים (474) ודירוג (4.0). חברו עם thenCombine והדפיסו 474 pages, rating 4.0.

Two tasks in parallel: a page count (474) and a rating (4.0). Combine with thenCombine and print 474 pages, rating 4.0.

איך הפלט אמור להיראות?What should the output look like?
474 pages, rating 4.0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: pages.thenCombine(rating, (p, r) -> ...)pages.thenCombine(rating, (p, r) -> ...)
לפתרוןSolution
תרגילExercise 3

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

join() זורק כי ההמרה נכשלה. הוסיפו exceptionally(e -> 0) לפני ה-join כך שיודפס year 0.

join() throws because the conversion failed. Add exceptionally(e -> 0) before the join so it prints year 0.

איך הפלט אמור להיראות?What should the output look like?
year 0
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: .exceptionally(e -> 0).exceptionally(e -> 0)
לפתרוןSolution
תרגילExercise 4

allOfallOf

לכל כותר משימה שמחשבת עמודים (אורך × 100). חכו לכולן עם CompletableFuture.allOf(...), ואז סכמו: total pages 1500.

A task per title computing pages (length × 100). Wait for them all with CompletableFuture.allOf(...), then sum: total pages 1500.

איך הפלט אמור להיראות?What should the output look like?
total pages 1500
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: allOf(all.toArray(new CompletableFuture[0]))allOf(all.toArray(new CompletableFuture[0]))
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

1000 שורות, כל ייבוא "מחכה לרשת" 20ms. משימה לכל שורה ב-newVirtualThreadPerTaskExecutor() (ב-try-with-resources). אספו Futures והדפיסו 1000 books imported, newest 1999. ברצף זה היה לוקח 20 שניות.

1000 lines, each import "waits for the network" 20ms. A task per line in newVirtualThreadPerTaskExecutor() (in try-with-resources). Collect Futures and print 1000 books imported, newest 1999. One by one it would take 20 seconds.

איך הפלט אמור להיראות?What should the output look like?
1000 books imported, newest 1999
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: סגירת ה-executor בסוף ה-try מחכה לכל המשימות.Closing the executor at the end of the try waits for every task.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

thenComposethenCompose

findId(title) ו-loadAuthor(id) מחזירות שתיהן CompletableFuture. שרשרו עם thenCompose (לא thenApply) והדפיסו Herbert עבור Dune.

findId(title) and loadAuthor(id) both return a CompletableFuture. Chain them with thenCompose (not thenApply) and print Herbert for Dune.

איך הפלט אמור להיראות?What should the output look like?
Herbert
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: עם thenApply הייתם מקבלים CompletableFuture<CompletableFuture<String>>.With thenApply you'd get a CompletableFuture<CompletableFuture<String>>.
לפתרוןSolution
אתגרChallenge 2

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

צרו thread וירטואלי עם Thread.ofVirtual().start(...) ו-thread רגיל עם Thread.ofPlatform().start(...). כל אחד מדפיס isVirtual(): virtual? true ואז virtual? false.

Create a virtual thread with Thread.ofVirtual().start(...) and a platform thread with Thread.ofPlatform().start(...). Each prints isVirtual(): virtual? true then virtual? false.

איך הפלט אמור להיראות?What should the output look like?
virtual? true
virtual? false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: join אחרי כל אחד שומר על הסדר.A join after each keeps the order.
לפתרוןSolution

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