Java Thread类的静态void sleep(long time_in_ms)方法,带示例
線程類靜態(tài)無效睡眠(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)方法適用于我們要在特定時(shí)間段內(nèi)(以毫秒為單位)停止當(dāng)前執(zhí)行線程的情況,換句話說,如果某個(gè)線程導(dǎo)致當(dāng)前線程在該方法中給出的毫秒級(jí)內(nèi)停止執(zhí)行一段時(shí)間,則該方法適用。
This method is static so we can access this method with class name too.
此方法是靜態(tài)的,因此我們也可以使用類名訪問此方法。
The return type of this method is void so it does not return anything.
此方法的返回類型為void,因此它不返回任何內(nèi)容。
This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get compiletime error.
該方法拋出一個(gè)InterruptedException異常,因此需要通過try-catch或throws來處理異常,否則我們將獲得編譯時(shí)錯(cuò)誤。
This method is overloaded.
此方法已重載。
For example, We have two threads [t1 – MyThread], [t2 – main] so will see what will happen.
例如,我們有兩個(gè)線程[ t1 – MyThread],[ t2 – main],因此將看到會(huì)發(fā)生什么。
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.
假設(shè)線程t1是否執(zhí)行,并且與此同時(shí),如果我們?cè)贛yThread中調(diào)用類似于/ * Thread.sleep(1000)* /的sleep(1000)方法,那么該線程將在1000毫秒內(nèi)停止執(zhí)行,并等待處理器和如果線程再次分配處理器,則同一線程將繼續(xù)執(zhí)行。
Syntax:
句法:
static void sleep(long time_in_ms){}Parameter(s):
參數(shù):
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.
當(dāng)我們編寫Thread.sleep(2000)時(shí) ,此行表示當(dāng)前正在執(zhí)行的線程將在2000毫秒內(nèi)停止其執(zhí)行,并且我們需要記住,同一線程將在調(diào)用sleep()方法的地方停止其執(zhí)行。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回類型為void ,它不返回任何內(nèi)容。
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
總結(jié)
以上是生活随笔為你收集整理的Java Thread类的静态void sleep(long time_in_ms)方法,带示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dbms标识符无效_DBMS中的聚合运算
- 下一篇: Java类类getConstructor