Java中的wait()和sleep()方法之間的區別

Java中的wait()和sleep()方法 (wait() and sleep() methods in Java)

First, we will see how wait() method differs from sleep() method in Java?

首先,我們將看到wait()方法與Java中的sleep()方法有何不同?

wait()方法 (wait() Method)

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This method is used to pause a Thread in Java.

    此方法用于暫停Java中的線程。

  • This method is defined in Object class.

    此方法在Object類中定義。

  • This method releases the monitor or acquired the lock on that monitor while waiting.

    此方法在等待時釋放監視器或獲得該監視器的鎖定。

  • wait() is a non-static method (i.e. instance method) so this method is accessible with the help of object class.

    wait()是一個非靜態方法(即實例方法),因此可以在對象類的幫助下訪問此方法。

  • Let suppose if our thread is waiting for execution so it wakes up at one condition is that when other calls notify() or notifyAll() method on the same object.

    讓我們假設,如果我們的線程正在等待執行,那么它在一種情況下被喚醒的原因是,當其他線程在同一對象上調用notify()notifyAll()方法時。

  • This method is useful for inter-thread communication.

    此方法對于線程間通信很有用。

  • In case of wait() method, waiting thread does not go into Runnable state directly (i.e. If waiting thread wake up then it first acquired the lock then after goes into Runnable state)

    在使用wait()方法的情況下,等待線程不會直接進入Runnable狀態(即,如果等待線程喚醒,則它首先獲取了鎖,然后進入Runnable狀態)

  • This method will be called from synchronized context only (i.e. we can call this method from either synchronize method or synchronized block).

    僅從同步上下文調用此方法(即,我們可以從syncize方法或synced塊調用此方法)。

  • In case of wait() method, Waiting for the thread will wait until a condition is true it is based on condition.

    如果使用wait()方法,則等待線程將一直等到條件為真(基于條件)。

  • The syntax of this method is given below :

    該方法的語法如下:

    final void wait(){}
    final void wait(long ms, int ns){}
    final void wait(long ms){}
    
    
  • This method is an overloaded method so we will see all the variations given below,

    此方法是重載方法,因此我們將看到下面給出的所有變化,

    1. wait()等待()
    2. wait(long ms)等待(長毫秒)
    3. wait(long ms, int ns)等待(長毫秒,整數ns)
  • We should go for wait() method if we want to wait for a certain condition.

    如果我們要等待某個條件,就應該使用wait()方法。

Second, we will see how sleep() method differs from the wait() method in Java?

其次,我們將看到sleep()方法與Java中的wait()方法有何不同?

sleep()方法 (sleep() Method)

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This method is used to pause a thread for a short duration in Java.

    此方法用于在Java中將線程暫停一小段時間。

  • This method is defined in Thread class.

    此方法在Thread類中定義。

  • This method does not release the monitor or acquired lock on that object while a thread is waiting.

    在線程等待時,此方法不會釋放監視器或對該對象的獲取鎖定。

  • sleep() is a static method (i.e. class method) so this method is accessible with Classname.

    sleep()是靜態方法(即類方法),因此可以使用Classname訪問此方法。

  • Let suppose if our thread is waiting for execution so it does not wake up at condition (i.e. we don't need to call notify() or notifyAll() method to wake up).

    假設我們的線程正在等待執行,因此它不會在條件下喚醒(即,我們不需要調用notify()notifyAll()方法來喚醒)。

  • This method is useful for when a thread wants to wait or sleep for a short duration.

    當線程想要短時間等待或Hibernate時,此方法很有用。

  • In case of sleep() method sleeping thread goes into Runnable state directly (i.e. If sleeping thread wakes up then it does not need to acquire the lock).

    如果使用sleep()方法,則睡眠線程直接進入Runnable狀態(即,如果睡眠線程喚醒,則不需要獲取鎖)。

  • This method will be called from non-synchronize context (i.e. we can call this method from non-synchronize method or block).

    該方法將從非同步上下文中調用(即,我們可以從非同步方法或塊中調用此方法)。

  • In the case of sleep() method, the sleeping thread will wait for a particular duration.

    對于sleep()方法,睡眠線程將等待特定的持續時間。

  • The syntax of this method is given below:

    該方法的語法如下:

    static void sleep(long ms){}
    static void sleep(long ms, int ns){}
    
    
  • This method is an overloaded method so we will see all the variations given below,

    此方法是重載方法,因此我們將看到下面給出的所有變化,

    1. sleep(long ms)睡眠(長毫秒)
    2. sleep(long ms, int ns)睡眠(長毫秒,整數ns)
  • We should go for sleep() method if we want to wait for a certain duration.

    如果我們要等待一定的時間,我們應該使用sleep()方法。

翻譯自: https://www.includehelp.com/java/differences-between-wait-and-sleep-methods-in-java.aspx

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

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

相關文章

離線使用iPhone SDK文檔的方法

在使用Xcode進行iPhone編程時,有時需要參考iPhone SDK的文檔,不過每次ControlClick后,Xcode都會試圖連接Internet,進行在線讀取。有什么方法能夠把資料下載到硬盤上進行離線閱讀嗎? 答案是肯定的。首先去Xcode的Prefer…

遠程連接sql server 2000服務器的解決方案

遠程連接sql server 2000服務器的解決方案2007-04-07 11:29遠程連接sql server 2000服務器的解決方案   一 看ping 服務器IP能否ping通。   這個實際上是看和遠程sql server 2000服務器的物理連接是否存在。如果不行,請檢查網絡,查看配置&#xff0c…

實訓09.10:HTML簡單表格設計

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>燕雨簡歷</title></head><body><table border"" cellspacing"" cellpadding"" width"400px" height"6…

LCD顯示實驗----STM32f4--HAL

步驟 LCD初始化 LCD_Init(); //LCD初始化此函數在lcd.c文件里面 2. 設置LCD背景顏色 LCD_Clear(WHITE);此函數在lcd.c文件里面 3. 設置字體顏色 POINT_COLORRED; 寫入要顯示的字體 LCD_ShowString(10,80,240,24,24,"LTDC TEST");LCD_ShowSt…

JavaScript | 使用提示從用戶輸入值

Example 1) Input name and print 示例1)輸入名稱和打印 Code (JS & HTML): 代碼(JS和HTML)&#xff1a; <!DOCTYPE html><HTML><HEAD><SCRIPT>var name prompt("Enter Your name:");var msg "Welcome "name;//alert(msg)…

一個游戲程序員的學習資料 (zz)

一個游戲程序員的學習資料//z 2012-4-19 14:39:51 PM IS2120CSDN想起寫這篇文章是在看侯杰先生的《深入淺出MFC》時, 突然覺得自己在大學這幾年關于游戲編程方面還算是有些心得&#xff0c;因此寫出這篇小文,介紹我眼中的游戲程序 員的書單與源代碼參考。一則是作為自己今后兩年…

項目管理中工作分解結構模型(WBSM)的應用

摘要 本文根據工作分解結構(WBS)的工作特點&#xff0c;運用系統工程的思想理論方法&#xff0c;構建了工作分解結構模型&#xff0c;并提出了模型算法;該模型方法的建立使得WBS工作更加簡單可靠、思路清晰、基于更加可靠的科學基礎之上。 1、工作分解結構模型(WBSM)方法工作程…

實訓09.11:java重點內容介紹

package test;/** * OP:面向對象的簡稱* 類&#xff1a;同一特征的屬性* * 類的定義&#xff1a;具有相同特征和行為的事物的抽象。&#xff08;不具體化&#xff09;* 對象&#xff08;實例對象&#xff09;&#xff1a;具體真實存在的實例。* 類是對象的實例&#xff1a;* *…

SPI通信原理---STM32F4--HAL

SPI接口原理 SPI是一種高速全雙工同步通信&#xff0c;在芯片管腳上占用四根線&#xff0c;主要應用在EEPROM、FLASH、實時時鐘、AD轉換器&#xff0c;還有數字信號處理器和數字信號解碼器之間。 SPI接口使用4根線通信。 MISO&#xff1a;主設備數據輸入&#xff0c;從設備數…

Linux 引導管理器 grub2 使用簡介

轉自&#xff1a;杜昌彬的空間 首先向其致敬&#xff01;有改動。 grub是Linux系統即其他類unix系統的主流bootloder&#xff0c;由于grub原來版本的設計存在很大缺陷&#xff0c;與以前的grub很不相同&#xff0c;其使用和配置也發生很大變化。現在很多Linux發行版本都使用了…

pata1015_ATA / PATA的完整形式是什么?

pata1015ATA / PATA&#xff1a;高級技術附件/并行高級技術附件 (ATA/PATA: Advanced Technology Attachment/Parallel Advanced Technology Attachment) ATA is an abbreviation of Advanced Technology Attachment. ATA has existed for a long time with the name PATA. Whe…

產品

總結一下&#xff1a;  1、核心功能要做透&#xff0c;做的人家追不上&#xff0c;自己的優勢要盡量的發揮&#xff1b;  2、產品口碑要建立&#xff0c;要關注高端用戶&#xff0c;要調整自己心態&#xff1b;  3、敏捷、快&#xff0c;產品迭代要快&#xff0c;快速實現…

FreeRTOS在STM32F429上移植

準備工作 FreeRTOS系統源碼基礎工程&#xff0c;這里我們用跑馬燈實驗 1.在工程里面添加FreeRTOS源碼 在工程里面新建一個名為FreeROTS的文件夾 將FreeRTOS源碼添加到這個文件夾里面 protable里面只需留下Keil、MemMang、RVDS文件夾 2、向工程分組中添加文件 FreeRTOS_C…

C++中的指針與引用(轉)

原文地址&#xff1a;http://www.cnblogs.com/skynet/archive/2010/09/22/1832911.html寫在前面 指針和引用形式上很好區別&#xff0c;但是他們似乎有相同的功能,都能夠直接引用對象&#xff0c;對其進行直接的操作。但是什么時候使用指針&#xff1f;什么時候使用引用呢&…

實訓09.11:數據庫一些簡單操作

new Database 新建數據庫 new Table 新建表 utf-8 編碼格式 primary key 主鍵&#xff1a;特點&#xff1a;在表中是唯一的不可重復的&#xff0c;一般都是學號&#xff0c;編號 auto increment 自增&#xff0c;一般都把主鍵設置為自增 allow nul…

c語言中將整數轉換成字符串_在C語言中將ASCII字符串(char [])轉換為八進制字符串(char [])...

c語言中將整數轉換成字符串Given an ASCII string (char[]) and we have to convert it into octal string (char[]) in C. 給定一個ASCII字符串(char [])&#xff0c;我們必須在C中將其轉換為八進制字符串(char [])。 Logic: 邏輯&#xff1a; To convert an ASCII string t…

Javascript的IE和Firefox兼容性匯編收藏.txt

document.form.item 問題 現有問題&#xff1a;現有代碼中存在許多 document.formName.item("itemName") 這樣的語句&#xff0c;不能在 MF 下運行 解決方法&#xff1a;改用 document.formName.elements["elementName"] 集合類對象問題 現有問題&#xff…

FreeRTOS系統配置文件FreeRTOSConfig.h

實際使用FreeRTOS的時候&#xff0c;我們時常需要根據自己需求來配置FreeRTOS&#xff0c;而且不同架構的MCU在使用的時候配置也不同&#xff0c;FreeRTOS的系統配置文件FreeRTOSConfig.h可以完成FreeRTOS的裁剪和配置。FreeRTOSConfig.h分成兩個部分&#xff0c;一個是INCLUDE…

SQL更新多條數據

問題&#xff1a;有兩個不同的表&#xff0c;其中都有一個編號的字段&#xff0c;而且存儲的內容是相同的&#xff0c;需要將一張表中的另外一些字段依據編號去與另一個表中編號對應來更新到另一個表中。 方法&#xff1a;由于在sql中是不支持同時更新多條包含編號的數據的&…

簡單的登錄系統(java+JFrame+Mysql)

連接數據庫 package 注冊信息; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class conn_db{ Connection con; String url null; Statement stmt; public void connection() throws ClassNotFoundException{ //…