日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Thread类的静态void sleep(long time_in_ms)方法,带示例

發布時間:2025/3/11 java 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Thread类的静态void sleep(long time_in_ms)方法,带示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

線程類靜態無效睡眠(long time_in_ms) (Thread Class static void sleep(long time_in_ms))

  • This method is available in package java.lang.Thread.sleep(long time_in_ms).

    軟件包java.lang.Thread.sleep(long time_in_ms)中提供了此方法。

  • sleep(long time_in_ms) method is applicable when we want to stop current executing thread for a particular amount of time in milliseconds or other words if a thread causes the current thread to stop executing for some time in milliseconds given in the method.

    sleep(long time_in_ms)方法適用于我們要在特定時間段內(以毫秒為單位)停止當前執行線程的情況,換句話說,如果某個線程導致當前線程在該方法中給出的毫秒級內停止執行一段時間,則該方法適用。

  • This method is static so we can access this method with class name too.

    此方法是靜態的,因此我們也可以使用類名訪問此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回類型為void,因此它不返回任何內容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get compiletime error.

    該方法拋出一個InterruptedException異常,因此需要通過try-catch或throws來處理異常,否則我們將獲得編譯時錯誤。

  • This method is overloaded.

    此方法已重載。

For example, We have two threads [t1 – MyThread], [t2 – main] so will see what will happen.

例如,我們有兩個線程[ t1 – MyThread],[ t2 – main],因此將看到會發生什么。

Let suppose if a thread t1 executes and in the meanwhile if we call sleep(1000) method like this /* Thread.sleep(1000)*/ inside MyThread so this thread will stop its execution for 1000 millisecond and will wait for the processor and if the thread allocates processor again then the same thread will continue its execution.

假設線程t1是否執行,并且與此同時,如果我們在MyThread中調用類似于/ * Thread.sleep(1000)* /的sleep(1000)方法,那么該線程將在1000毫秒內停止執行,并等待處理器和如果線程再次分配處理器,則同一線程將繼續執行。

Syntax:

句法:

static void sleep(long time_in_ms){}

Parameter(s):

參數:

When we write Thread.sleep(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds and we need to remember the same thread will stop its execution from where sleep() method is called.

當我們編寫Thread.sleep(2000)時 ,此行表示當前正在執行的線程將在2000毫秒內停止其執行,并且我們需要記住,同一線程將在調用sleep()方法的地方停止其執行。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回類型為void ,它不返回任何內容。

Java程序,演示sleep()方法的示例 (Java program to demonstrate example of sleep() method)

/* We will use Thread class methods so we are importing the package but it is not mandate because it is imported by default */import java.lang.Thread;class MyThread extends Thread {//Override run() method of Thread class public void run() {for (int i = 0; i < 5; ++i) {System.out.println("Thread started:" + Thread.currentThread().getName());try {Thread.sleep(2000);} catch (Exception ex) {System.out.println(ex.getMessage());}}System.out.println("Thread Ended :" + Thread.currentThread().getName());} }class MainThread1 {public static void main(String[] args) throws Exception {MyThread mt = new MyThread();mt.start();for (int j = 0; j < 2; ++j)System.out.println("Thread started:" + Thread.currentThread().getName());System.out.println("Thread ended:" + Thread.currentThread().getName());} }

Output

輸出量

E:\Programs>javac Main.javaE:\Programs>java Main Thread started:main Thread started:Thread-0 Thread started:main Thread ended:main Thread started:Thread-0 Thread started:Thread-0 Thread started:Thread-0 Thread started:Thread-0 Thread Ended :Thread-0

翻譯自: https://www.includehelp.com/java/thread-class-static-void-sleep-long-time_in_ms-method-with-example.aspx

總結

以上是生活随笔為你收集整理的Java Thread类的静态void sleep(long time_in_ms)方法,带示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。