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.mzph.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{ //…