OLHub

Concurrency cơ bản

Thread, Runnable, synchronized, volatile, ExecutorService, CompletableFuture, concurrent collections, virtual threads (Java 21). Hiểu memory model, happens-before, và vì sao concurrency khó hơn bạn nghĩ.

6 bài · ~149 phútMiễn phí

Nội dung

Danh sách bài học

  1. 01

    Thread cơ bản — chạy song song, start/join/interrupt

    Thread và Runnable, lifecycle 6 trạng thái, start vs run, join, interrupt cooperative. Vì sao single-thread không đủ khi gặp I/O, thread sinh ra từ đâu trong JVM, và tại sao Thread.stop() bị deprecated.

    ~22 phút
  2. 02

    synchronized và volatile — memory model và happens-before

    Atomicity vs visibility, synchronized là mutex + visibility, volatile chỉ visibility, khái niệm happens-before. Vì sao counter++ không safe dù thêm volatile, và làm cách nào JVM/CPU cache biến khiến thread A không thấy ghi của thread B.

    ~28 phút
  3. 03

    ExecutorService và CompletableFuture — thread pool và async

    Vì sao không dùng new Thread() trong production. Thread pool với ExecutorService, 4 factory, Future.get() blocking. CompletableFuture chain async không callback hell, thenApply vs thenCompose, exceptionally.

    ~25 phút
  4. 04

    Concurrent collections — ConcurrentHashMap, CopyOnWrite, BlockingQueue

    Vì sao HashMap có thể infinite loop dưới multithread. Lock striping của ConcurrentHashMap, copy-on-write cho read-heavy list, BlockingQueue cho producer-consumer. Back-pressure pattern.

    ~22 phút
  5. 05

    Virtual threads — Java 21 lightweight concurrency

    Project Loom, JEP 444 Java 21. Platform thread tốn 1MB, virtual thread ~300 byte. Triệu thread trong 1 JVM. Carrier thread, mount/unmount, pinning. Khi nào dùng, khi nào KHÔNG dùng.

    ~22 phút
  6. 06

    Mini-challenge: Concurrent price aggregator

    Bài thực hành khép lại Module 10 — gọi 5 'API' song song với CompletableFuture, timeout, fallback, aggregate kết quả. Mô phỏng backend thực tế.

    ~30 phút