


重點:BLOCKED 狀態由同步鎖、I/O、sleep 引起;yield() 讓出 CPU 進入 RUNNABLE。
synchronized 獲取。synchronized 塊中調用,釋放/獲取 monitor。

Thread:start()、sleep()、yield()、join()、interrupt()。Runnable:無返回值。Callable:有返回值,配合 Future(get() 阻塞、isDone() 非阻塞)。AtomicInteger 等,CAS 樂觀鎖,compareAndSet 實現無鎖線程安全。ReentrantLock:可重入,支持可中斷鎖 lockInterruptibly()。ReentrantReadWriteLock:讀讀共享,寫寫/讀寫互斥。BlockingQueue:put/take 阻塞,生產消費神器(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、SynchronousQueue)。ConcurrentHashMap:高性能線程安全 Map,分段鎖/JDK8 CAS+synchronized。ThreadPoolExecutor 核心參數:corePoolSize、maximumPoolSize、keepAliveTime、workQueue、threadFactory。工廠方法:Executors.newFixedThreadPool 等。

jstack 排查。Thread.setDefaultUncaughtExceptionHandler。finally,即使 OOM 也能執行。學習交流群:279558494