Thread.sleep(1000) 與 thread.sleep(1000)(thread 為 Thread 實例)語義完全一致:均調用 Thread.sleep(long) 靜態方法,令當前執行線程休眠。但 IDE 會對實例調用報警告:The static method sleep(long) from the type Thread should be accessed in a static way。
thread.sleep() 讓 thread 對象代表的線程休眠的 APIThread.sleep(),語義清晰、無警告Thread t = new Thread(() -> { ... });
t.sleep(1000); // 錯誤理解:以為讓 t 線程睡,實際讓主線程睡
run() 內部調用 Thread.sleep()TimeUnit.MILLISECONDS.sleep(1000) 可讀性更好thread.interrupt() 拋出 InterruptedException