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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2023/12/1 java 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 _thread_in_vm_Java Thread类的静态void sleep(long time_in_ms,int time_in_ns)方法,带示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

_thread_in_vm

線程類靜態(tài)無效睡眠(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)方法適用于當(dāng)我們想要以毫秒+納秒(即,額外的時(shí)間以納秒為單位)的特定時(shí)間量停止當(dāng)前正在執(zhí)行的線程時(shí),或者換句話說,如果線程導(dǎo)致當(dāng)前線程停止該方法執(zhí)行的時(shí)間以毫秒+納秒為單位。

  • This method is static so we can access this method with the 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 a compile-time error.

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

  • 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.

    我們?cè)诖颂幫ㄟ^Thread類的給定方法傳遞兩個(gè)參數(shù),該參數(shù)將是time_in_ms(以毫秒為單位的時(shí)間)和time_in_ns(以納秒為單位的時(shí)間),這兩個(gè)參數(shù)是線程Hibernate的持續(xù)時(shí)間,因此該線程等待ms + ns時(shí)間。

  • 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.

    如果其他線程執(zhí)行的時(shí)間較少,那么在這種情況下,如果我們調(diào)用sleep()方法,則有可能完成該線程,因?yàn)楫?dāng)前線程將等待time_in_ms + time_in_ms 。

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,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.

假設(shè)是否執(zhí)行了線程t1,同時(shí)在MyThread中調(diào)用了像這樣的sleep(1000,500)方法/ * Thread.sleep(1000,500)* /,那么該線程將在1000毫秒和500納秒內(nèi)停止執(zhí)行將等待處理器,并且如果線程再次分配處理器,則同一線程將繼續(xù)執(zhí)行。

Syntax:

句法:

static void sleep(long time_in_ms, int time_in_ns){}

Parameter(s):

參數(shù):

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.

當(dāng)我們編寫Thread.sleep(2000,1000)時(shí) ,此行表示當(dāng)前正在執(zhí)行的線程將在2000毫秒內(nèi)停止執(zhí)行,另外1000納秒,我們需要記住同一線程將在調(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 < 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.javaE:\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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。