1main tạo ctx bằng signal.NotifyContext (Ctrl+C sẽ huỷ) và danh sách hai OrderPlaced A1, A2, rồi gọi queue := notify.Produce(ctx, orders).main creates ctx with signal.NotifyContext (Ctrl+C cancels it) and a list of two OrderPlaced, A1 and A2, then calls queue := notify.Produce(ctx, orders).
2Produce tạo make(chan OrderPlaced, 100) và một goroutine gửi out <- o cho từng đơn. Buffer còn chỗ nên gửi xong ngay; khi buffer đầy, lệnh gửi sẽ chờ (backpressure) hoặc thoát nếu ctx bị huỷ.Produce makes make(chan OrderPlaced, 100) and a goroutine that sends out <- o for each order. The buffer has room so the sends finish at once; when it is full the send waits (backpressure) or exits if ctx is cancelled.
3Hết danh sách, goroutine Produce chạy defer close(out). Chỉ chủ của channel được đóng nó, và đóng không xoá các đơn còn trong buffer.With the list exhausted, the Produce goroutine runs defer close(out). Only the channel's owner may close it, and closing does not discard orders still in the buffer.
4main gọi notify.Consume(ctx, queue, logMailer{}, 3): wg.Add(1) và chạy ba goroutine consumer, mỗi cái for o := range in. Consumer 0 nhận A1; hai consumer kia không nhận bản sao của A1.main calls notify.Consume(ctx, queue, logMailer{}, 3): wg.Add(1) and three consumer goroutines, each doing for o := range in. Consumer 0 receives A1; the other two do not get a copy of A1.
5Consumer 0 gọi m.SendReceipt(ctx, o.Email, o.OrderID, o.Amount) qua interface Mailer; logMailer in biên nhận. Cùng lúc consumer 1 nhận A2. Thứ tự in giữa các consumer không được bảo đảm.Consumer 0 calls m.SendReceipt(ctx, o.Email, o.OrderID, o.Amount) through the Mailer interface; logMailer prints the receipt. Meanwhile consumer 1 receives A2. Print order across consumers is not guaranteed.
6Consumer 1 in biên nhận A2. Channel đã đóng và buffer đã cạn nên cả ba vòng range kết thúc, defer wg.Done() chạy, wg.Wait() trả về và main kết thúc. Lỗi từ SendReceipt (nếu có) chỉ được log "consumer %d: %v".Consumer 1 prints the A2 receipt. The channel is closed and drained, so all three range loops end, defer wg.Done() runs, wg.Wait() returns and main exits. Errors from SendReceipt (if any) are only logged as "consumer %d: %v".
7Java: QueueConfig.orderQueue() tạo ArrayBlockingQueue(1_000). CheckoutService.placeOrder lưu đơn rồi queue.offer(event, 200, MILLISECONDS); queue vẫn đầy sau 200ms thì ném lỗi để không chặn thread HTTP mãi.Java: QueueConfig.orderQueue() creates an ArrayBlockingQueue(1_000). CheckoutService.placeOrder saves the order and calls queue.offer(event, 200, MILLISECONDS); if the queue is still full after 200ms it throws so the HTTP thread is never blocked forever.
8EmailConsumer.start() (@PostConstruct) chạy 4 virtual thread email-0..3. Mỗi thread queue.take() (chờ khi queue rỗng) rồi email.sendReceipt(...); RuntimeException chỉ được log để một tin hỏng không giết consumer.EmailConsumer.start() (@PostConstruct) starts 4 virtual threads email-0..3. Each does queue.take() (waiting while the queue is empty) then email.sendReceipt(...); a RuntimeException is only logged so one bad message does not kill the consumer.
9Khi tắt ứng dụng, @PreDestroy stop() gọi workers.forEach(Thread::interrupt). take() ném InterruptedException, worker khôi phục cờ interrupt và vòng while (!isInterrupted()) thoát.On shutdown, @PreDestroy stop() calls workers.forEach(Thread::interrupt). take() throws InterruptedException, the worker restores the interrupt flag and the while (!isInterrupted()) loop exits.