1. 程式人生 > >Thread.sleep()和Thread.currentThread().sleep()區別

Thread.sleep()和Thread.currentThread().sleep()區別

先看一下程式碼

public class Thread1 extends Thread{
    
    @Override
    public void run() {
        try {
            System.out.println("Start");
            //Thread.sleep(5000);
            Thread.currentThread().sleep(5000);
            System.out.println("Finished");
        } catch (InterruptedException e) {
            
// TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Thread1 t1 = new Thread1(); t1.start();

在編譯器裡執行看一下,沒什麼區別,但是在程式設計規範中,“呼叫一個例項的靜態方法,而實際上應該是呼叫一個類的靜態方法”,所以還是用Thread.sleep()最好