當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Thread.sleep()用法及代碼示例

線程類是本質上是程序執行線程的類。它存在於Java.lang包中。線程類包含Sleep()方法。線程類中存在兩種Sleep()方法的重載方法,一種方法帶有一個自變量,另一種方法帶有兩個自變量。 sleep()方法用於在特定時間段內停止執行當前線程(無論係統中正在執行的線程),並在該時間段結束後,較早執行的線程再次開始執行。

關於Thread.sleep()方法的要點:

  • 方法每當執行Thread.sleep()函數時,它將始終暫停當前線程的執行。
  • 如果其他任何線程在睡眠時中斷,則將拋出InterruptedException。
  • 如果係統忙, 那麽線程實際進入睡眠的時間會更多 與過去相比呼喚睡眠方法以及係統 負載少 然後實際睡覺時間的線程將接近那個在調用sleep()方法時傳遞。

Sleep()方法的語法

  • public static void sleep(long millis)throws InterruptedException   
  • public static void sleep(long millis)throws IllegalArguementException
  • public static void sleep(long millis, int nanos)throws InterruptedException  
  • public static void sleep(long millis, int nanos)throws  IllegalArguementException

Thread.Sleep()方法中傳遞的參數

  • millis:線程將休眠的持續時間(以毫秒為單位)
  • nanos:這個我們想要的額外時間(以納秒為單位)線程睡覺。 取值範圍是0到999999。

Sleep()的返回類型方法:它不返回任何值,IE睡眠函數的返回類型為void。



具有一個參數的sleep方法是本機方法,即,該方法的實現是用另一種編程語言完成的,而具有兩個參數的另一種方法不是本機方法,即,其實現是用Java完成的。這兩個sleep方法都是靜態的,即我們可以使用Thread類訪問它們。兩種方法都拋出一個檢查異常,即我們可以使用throws關鍵字或使用try and catch塊來處理異常。

我們可以對任何線程使用Thread.Sleep()方法,即可以與主線程或以編程方式創建的任何其他線程一起使用。

  • 對主線程使用Thread.Sleep()方法

Java

// Java Program for sleeping the main thread. 
  
import java.io.*; 
import java.lang.Thread; 
  
class GFG { 
    public static void main(String[] args) 
    { 
        // we can also use throws keyword foloowed by 
        // exception name for throwing the exception 
        
        try { 
            for (int i = 0; i < 5; i++) { 
                
                // it will sleep the main thread for 1 sec 
                // ,each time the for loop runs 
                Thread.sleep(1000); 
                
                // printing the value of the variable 
                System.out.println(i); 
            } 
        } 
        catch (Exception e) { 
            
            // catching the exception 
            System.out.println(e); 
        } 
    } 
}
輸出
0
1
2
3
4
  • 對自定義線程使用Thread.Sleep()方法

Java

// Java Program for sleeping the custom thread. 
  
import java.io.*; 
import java.lang.Thread; 
  
class GFG extends Thread { 
  
    public void run() 
    { 
        // thread 0 
  
        // we can also use throws keyword foloowed by 
        // exception name for throwing the exception 
        try { 
            for (int i = 0; i < 5; i++) { 
                
                // it will sleep the main thread for 1 sec 
                // ,each time the for loop runs 
                Thread.sleep(1000); 
                
                // This Thread.sleep() method will sleep the 
                // thread 0. 
                // printing the value of the variable 
                System.out.println(i); 
            } 
        } 
        catch (Exception e) { 
            
            // catching the exception 
            System.out.println(e); 
        } 
    } 
    public static void main(String[] args) 
    { 
        // main thread 
        GFG obj = new GFG(); 
        obj.start(); 
    } 
}
輸出
0
1
2
3
4
  • 睡眠時間為負數時發生IllegalArguementException

Java

// Java Program for showing how exception can occur if we 
// pass the negative timeout value. 
  
import java.io.*; 
import java.lang.Thread; 
  
class GFG { 
    public static void main(String[] args) 
    { 
        // we can also use throws keyword foloowed by 
        // exception name for throwing the exception 
        
        try { 
            for (int i = 0; i < 5; i++) { 
                
                // this will throw the 
                // IllegalArgumentException 
                Thread.sleep(-100); 
                
                // printing the value of the variable 
                System.out.println(i); 
            } 
        } 
        catch (Exception e) { 
            
            // catching the exception 
            System.out.println(e); 
        } 
    } 
}
輸出
java.lang.IllegalArgumentException:timeout value is negative

相關用法


注:本文由純淨天空篩選整理自lavishgarg26大神的英文原創作品 Thread.sleep() Method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。