_thread_in_vm
線程類靜態無效睡眠(long time_in_ms,int time_in_ns) (Thread Class static void sleep(long time_in_ms, int time_in_ns))
This method is available in package java.lang.Thread.sleep(long time_in_ms, int time_in_ns).
軟件包java.lang.Thread.sleep(long time_in_ms,int time_in_ns)中提供了此方法。
sleep(long time_in_ms, int time_in_ns) method is applicable when we want to stop current executing thread for particular amount of time in milliseconds + nanoseconds (i.e. with additional time in nanoseconds) as well or in other words if a thread causes current thread to stop executing for some time in milliseconds + nanoseconds given in the method.
sleep(long time_in_ms,int time_in_ns)方法適用于當我們想要以毫秒+納秒(即,額外的時間以納秒為單位)的特定時間量停止當前正在執行的線程時,或者換句話說,如果線程導致當前線程停止該方法執行的時間以毫秒+納秒為單位。
This method is static so we can access this method with the 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 a compile-time error.
該方法拋出InterruptedException異常,因此需要通過try-catch或throws來處理異常,否則我們將獲得編譯時錯誤。
We pass here two parameters in the given method of the Thread class and the parameter will be time_in_ms(time in milliseconds) and time_in_ns(time in nanoseconds) this two-parameter is the duration of time to sleep of our Thread so this thread wait for ms+ns time.
我們在此處通過Thread類的給定方法傳遞兩個參數,該參數將是time_in_ms(以毫秒為單位的時間)和time_in_ns(以納秒為單位的時間),這兩個參數是線程Hibernate的持續時間,因此該線程等待ms + ns時間。
If other thread takes less time to executes so in that case if we call sleep() method then there is a possibility of completion of the Thread because the current thread will wait for time_in_ms + time_in_ms.
如果其他線程執行的時間較少,那么在這種情況下,如果我們調用sleep()方法,則有可能完成該線程,因為當前線程將等待time_in_ms + time_in_ms 。
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,500) method like this /* Thread.sleep(1000,500)*/ inside MyThread so this thread will stop its execution for 1000 millisecond and 500 nanoseconds will wait for the processor and if the thread allocates processor again then the same thread will continue its execution.
假設是否執行了線程t1,同時在MyThread中調用了像這樣的sleep(1000,500)方法/ * Thread.sleep(1000,500)* /,那么該線程將在1000毫秒和500納秒內停止執行將等待處理器,并且如果線程再次分配處理器,則同一線程將繼續執行。
Syntax:
句法:
static void sleep(long time_in_ms, int time_in_ns){
}
Parameter(s):
參數:
When we write Thread.sleep(2000,1000) so this line means currently executing thread will stop its execution for 2000 milliseconds and additional 1000 nanoseconds we need to remember the same thread will stop its execution from where sleep() method is called.
當我們編寫Thread.sleep(2000,1000)時 ,此行表示當前正在執行的線程將在2000毫秒內停止執行,另外1000納秒,我們需要記住同一線程將在調用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 < 2; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
try {
Thread.sleep(1000, 500);
} 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 < 5; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
Output
輸出量
E:\Programs>javac Main.java
E:\Programs>java Main
Thread started:main
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread Ended :Thread-0
翻譯自: https://www.includehelp.com/java/thread-class-static-void-sleep-long-time_in_ms-int-time_in_ns-method-with-example.aspx
_thread_in_vm