1ProductPageService.load("SKU-42") gọi CompletableFuture.supplyAsync(() -> catalog.find(sku), ioPool). Lời gọi trả productF ngay, còn việc thật chạy trên virtual thread do AsyncConfig.ioPool() cấp.ProductPageService.load("SKU-42") calls CompletableFuture.supplyAsync(() -> catalog.find(sku), ioPool). It returns productF at once, while the real work runs on a virtual thread provided by AsyncConfig.ioPool().
2Trước khi chờ bất kỳ kết quả nào, load tạo tiếp priceF = supplyAsync(pricing.priceFor(sku)).orTimeout(300ms) và reviewsF = supplyAsync(reviews.top(sku, 5)).exceptionally(ex -> List.of()), để ba khoảng chờ chồng lên nhau.Before waiting on anything, load creates priceF = supplyAsync(pricing.priceFor(sku)).orTimeout(300ms) and reviewsF = supplyAsync(reviews.top(sku, 5)).exceptionally(ex -> List.of()), so the three waits overlap.
3Mỗi task tự hoàn tất Future của mình: reviews xong ở 150ms, catalog ở 200ms, pricing ở 250ms (trước timeout 300ms). Nếu reviews lỗi, exceptionally thay bằng List.of(); pricing lỗi thì priceF lỗi theo.Each task completes its own Future: reviews at 150ms, catalog at 200ms, pricing at 250ms (inside the 300ms timeout). If reviews fail, exceptionally substitutes List.of(); if pricing fails, priceF fails.
4CompletableFuture.allOf(productF, priceF, reviewsF).join() trả về, rồi new ProductPage(productF.join(), priceF.join(), reviewsF.join()). Tổng thời gian khoảng 251ms thay vì 600ms tuần tự.CompletableFuture.allOf(productF, priceF, reviewsF).join() returns, then new ProductPage(productF.join(), priceF.join(), reviewsF.join()). Total time is about 251ms instead of 600ms sequentially.
5Go Service.Load làm cùng việc bằng errgroup: WithTimeout 800ms, ba g.Go ghi page.Product, page.Price, page.Reviews. Nhánh reviews lỗi thì gán rs = nil và return nil để không làm hỏng cả trang; g.Wait() chờ đủ ba nhánh.Go Service.Load does the same with errgroup: WithTimeout 800ms, three g.Go calls write page.Product, page.Price and page.Reviews. If reviews fail it sets rs = nil and returns nil so the page does not fail; g.Wait() waits for all three.
6QuoteController.quote(cart) không chặn thread: subtotalF = supplyAsync(pricing.subtotal(cart)), shippingF = supplyAsync(ghn.fee(cart.address())), rồi subtotalF.thenCombine(shippingF, (s, f) -> new Quote(s, f, s + f)).QuoteController.quote(cart) never blocks a thread: subtotalF = supplyAsync(pricing.subtotal(cart)), shippingF = supplyAsync(ghn.fee(cart.address())), then subtotalF.thenCombine(shippingF, (s, f) -> new Quote(s, f, s + f)).
7Go Service.Quote dùng Future tự viết: Async(ctx, fn) tạo Future[T]{ch: make(chan result[T], 1)} và một goroutine chạy fn rồi f.ch <- result. Buffer 1 cho phép goroutine gửi xong và thoát dù chưa ai Await.Go Service.Quote uses the hand-written Future: Async(ctx, fn) creates Future[T]{ch: make(chan result[T], 1)} and a goroutine that runs fn then does f.ch <- result. Buffer 1 lets the goroutine send and exit before anyone calls Await.
8subF.Await(ctx) select giữa <-f.ch và <-ctx.Done(); có lỗi thì trả Quote{} ngay. Sau đó shipF.Await(ctx), cuối cùng trả Quote{Subtotal: sub, Shipping: ship, Total: sub + ship}.subF.Await(ctx) selects between <-f.ch and <-ctx.Done(); on error it returns Quote{} immediately. Then shipF.Await(ctx), and finally it returns Quote{Subtotal: sub, Shipping: ship, Total: sub + ship}.