_thread_in_vm_Java Thread類的靜態void sleep(long time_in_ms,int time_in_ns)方法,帶示例

_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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376968.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376968.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376968.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

大規模web服務開發技術(轉)

前段時間趁空把《大規模web服務開發技術》這本書看完了&#xff0c;今天用一下午時間重新翻了一遍&#xff0c;把其中的要點記了下來&#xff0c;權當復習和備忘。由于自己對數據壓縮、全文檢索等還算比較熟&#xff0c;所以筆記內容主要涉及前5章內容&#xff0c;后面的零星記…

IO多路復用的三種機制Select,Poll,Epoll

IO多路復用的本質是通過系統內核緩沖IO數據讓單個進程可以監視多個文件描述符&#xff0c;一旦某個進程描述符就緒(讀/寫就緒)&#xff0c;就能夠通知程序進行相應的讀寫操作。 select poll epoll都是Linux提供的IO復用方式&#xff0c;它們本質上都是同步IO&#xff0c;因為它…

qt中按鈕貼圖

一.QT之QPushButton按鈕貼圖 二.QT之QToolButton按鈕貼圖 一.QT之QPushButton按鈕貼圖具體操作流程 1. Qt Designer中拖入一Tool Button 2. 選擇圖標的圖片放入工程目錄下&#xff0c;如放在Resources內 3. 雙擊工程的Resource Files下的qrc文件&#xff0c;如圖 4. 在彈出的窗…

Ubuntu手動編譯gVim7.3修復終端啟動時與ibus的沖突

個bug伴隨著Ubuntu/ibus的升級苦憋已久&#xff0c;癥狀為終端啟動gvim時卡死&#xff0c;gvim -f可以緩解此問題&#xff0c;但偶爾還是要發作&#xff0c;況且每次末尾托個&也不方便。其實新版gvim已經修復此bug&#xff0c;不過ubuntu安裝包一直沒更新&#xff0c;那我們…

Android Activity類講解(一)

--by CY[kotomifigmail.com] &#xff11;&#xff0e;protected void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!");   } 當創建一個Activity時&#xff0c;系統會自動調用onCreate方法來完成創建工作&#xff0e;該創建工作包括布…

Mysql的undo、redo、bin log分析

目錄關于undo log關于redolog關于binlog一個事務的提交流程undo log :記錄數據被修改之前的樣子 redo log&#xff1a;記錄數據被修改之后的樣子 bin log&#xff1a;記錄整個操作。 關于undo log 關于undo log&#xff1a; 在執行一條涉及數據變更的sql時&#xff0c;在數據…

typedef 字符串_typedef在C中使用字符數組(定義別名來聲明字符串)的示例

typedef 字符串Here, we have to define an alias for a character array with a given number of maximum characters length to read strings? 在這里&#xff0c;我們必須為具有給定最大字符長度數的字符數組定義別名&#xff0c;以讀取字符串 &#xff1f; In the below-…

最小堆實現代碼

參考算法導論、數據結構相關書籍&#xff0c;寫得最小堆實現的源代碼如下&#xff1a; 1 //2 //--最小堆實例3 //4 5 #include <iostream>6 #include <vector>7 #include <string>8 using namespace std;9 10 template<typename Comparable>11 class m…

非常好的在網頁中顯示pdf的方法

今天有一需求&#xff0c;要在網頁中顯示pdf&#xff0c;于是立馬開始搜索解決方案&#xff0c;無意中發現一個非常好的解決方法&#xff0c;詳見http://blogs.adobe.com/pdfdevjunkie/web_designers_guide。 其實就光看這個網站也足夠了&#xff0c;http://www.pdfobject.com/…

Redis字典實現、Hash鍵沖突以及漸進式rehash

本筆記參考《Redis設計與實現》 P24~ 37 目錄Redis字典實現哈希表節點結構哈希表結構字典哈希算法解決hash沖突rehash漸進式hashRedis字典實現 哈希表節點結構 typedef struct dictEntry {// 鍵void *key;// 值 : 可以是一個指針&#xff0c;或者是一個uint64/int64 的整數un…

Java線程類void setContextClassLoader(ClassLoader loader)方法,帶示例

線程類void setContextClassLoader(ClassLoader loader) (Thread Class void setContextClassLoader(ClassLoader loader)) This method is available in package java.lang.Thread.setContextClassLoader(ClassLoader loader). 軟件包java.lang.Thread.setContextClassLoader(…

JPA概要

本文最新版已更新至&#xff1a;http://thinkinside.tk/2012/12/30/JPA.html JPA定義了Java ORM及實體操作API的標準。本文摘錄了JPA的一些關鍵信息以備查閱。 如果有hibernate的基礎&#xff0c;通過本文也可以快速掌握JPA的基本概念及使用。 Table of Contents 1 JPA概述2 實…

如何配置能讓fiddler抓去https的請求?

1、打開fiddler&#xff0c;>>Tools>>Fiddler Options&#xff0c; 打開如圖所示的HTTPS配置項&#xff1a;點擊Export Rppt Certifica to Desktop :桌面上多了一個證書&#xff1a;下面就是將證書導入&#xff1a;點擊開始-運行&#xff0c;輸入&#xff1a;mmc,…

Redis對象的refcount與lru屬性(內存回收、對象共享、空轉時長)

本筆記參考《Redis設計與實現》 P84~P88 內存回收 Redis在對象系統中使用reference counting技術實現了內存回收機制。程序可以通過跟蹤對象的引用計數信息&#xff0c;在適當的時候自動釋放對象并進行內存回收。 typedef struct redisObject {// ...// 引用計數int refcoun…

【閑聊】Baidu Map,excellent !!!Diaoyv island is China 's

【釣魚島】釣魚島是中國的&#xff01;Diaoyu Islands are Chinas! 釣魚島は中國のアール! ————————————youngLaker轉載于:https://www.cnblogs.com/younglaker/archive/2012/12/31/2840190.html

08:vigenère密碼_密碼技術:Vigenére密碼,Playfair密碼,Hill密碼

08:vigenre密碼1)Vigenre密碼 (1) Vigenre Cipher) This technique is an example of Polyalphabetic Substitution technique which uses 26 Caesar ciphers make up the mono-alphabetic substitution rules which follow a count shifting mechanism from 0 to 25. That is,…

Redis的RDB文件與AOF文件

本筆記參考《Redis設計與實現》 P118 ~ P150 RDB文件 1、RDB文件用于保存和還原Redis服務器所有數據庫中的所有鍵值對數據 2、SAVE命令由服務器進程直接執行保存操作&#xff0c;該命令會阻塞服務器 3、BGSAVE命令由子進程執行保存操作&#xff0c;不會阻塞服務器 注意此時服…

eclipse擴容

eclipse擴容 -vmD:/jdk-6u17-windows-i586/jdk1.6.0_17/bin/javaw.exe-startupplugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar-nlen_US--launcher.libraryplugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.200.v20120913-144807-productorg.eclipse…

node oauth2驗證_如何設置和使用護照OAuth Facebook身份驗證(第2部分)| Node.js

node oauth2驗證In my last article (How to set up and use passport OAuth Facebook Authentication (Section 1) | Node.js), we looked at another form of authentication called the OAuth authentication which involves sign in or signup using social media. 在我的上…

Python and Microsoft Word

國外網站看到的文章&#xff1a;Accessing Microsoft Word with Python follows the same syntax that we used for Excel. Let’s take a quick look at how to access Word.from time import sleep import win32com.client as win32RANGE range(3, 8)def word():word win32…