当调用sleep ,yield 的时候,并不释放所拥有的锁,
而调用wait的时候,则释放所拥有的锁,
Thread.sleep(long milliseconds): sends the current thread into Non-Runnable state for the specified amount of time. But, this doesn’t cause the thread to loose ownership of the acquired monitors. So, if the current thread is into a synchronized block/method, then no other thread will be able to enter that block/method. This method throws ‘InterruptedException’ if another thread interrupts it. There is another variant of the ‘sleep()’ method as well where it accepts two arguments – one, long milliseconds, and second, int nanoseconds. Evidently, it causes the current thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds. The second argument ‘int nanoseconds’ can acquire a value of the range 0-999999. Another noticeable difference between the two variants is that sleep(long milliseconds, int nanoseconds) throws an IllegalArgumentException if either the value of milliseconds is negative or the value of nanoseconds is not in the range 0-999999. As is the case with the first variant, it throws InterruptedException is another thread interrupts it. Common Error: both these methods are static, so even if you try to call ‘t.sleep(…)’ on a different thread, it’ll be called on the current thread only and not on the ‘t’ thread. In fact, one should avoid calling any static method on an object reference. wait() method: There are three variants of this method in the ‘Object’ class:- public final void wait(long timeout) public final void wait(long timeout, int nanoseconds) public final void wait() All the three methods throw InterruptedException & IllegalMonitorStateException. The first two may also throw IllegalArgumentException. The wait() method also sends the current thread into Non-Runnable state like the sleep() method. But, the difference between the two is that in case of ‘wait()’ the locks are released before going into Non-Runnable state. Another apparent difference is that ‘wait()’ is an instance method, while sleep() is a static method. The method ‘wait()’ should be called for an object only when the current thread has already acquired lock for that object. This causes the current thread to wait either another thread invokes the ‘notify()’ method or the ‘notifyAll()’ method for this object, or a specified amount of time has elapsed and after that the thread starts participating in thread scheduling process to acquire the monitor of the object to proceed further. The wait() method causes the current thread to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. After the execution of this method invocation, the thread becomes disabled for any scheduling purposes and lies dormant until one of the following things happen:- Any other thread invokes ‘notify()’ method this object and the thread under consideration is arbitrarily chosen as the thread to be awakened. Any other thread invokes ‘notifyAll()’ for this object. Any other thread interrupts the thread under consideration. The specified amount of time has elapsed (in case first two variants of wait() are used) After any of the four above mentioned events happens, the thread is removed from the wait set for this object and re-enabled for thread scheduling. It’ll compete for the rights to synchronize on the object in an usual manner and it’ll keep doing this until it acquires control on the object and gains all the synchronization claims on the object, which it had acquired at the time of ‘wait()’ invocation. This means that after the return from wait() method, the synchronization state of object and of the thread will be exactly the same as it was before the wait() invocation.
分享到:
相关推荐
Difference Be...Pronunciation_马生虎.caj
**ModelMaker Structured Difference Viewer v1.5.0.173** 是一款专为软件开发者和数据模型设计者设计的工具,它主要用于比较和理解两个结构化模型之间的差异。这款工具的核心功能在于帮助用户清晰地识别出在数据库...
A computer's architecture is its abstract model and is the programmer's view in terms of instructions, addressing modes and registers. A computer's organization expresses the realization of the ...
有限差分法(Finite Difference Method, FDM)是数值分析领域中解决偏微分方程(Partial Differential Equations, PDEs)的一种常用方法,尤其适用于处理拟线性和非线性超几何方程。在标题"finite difference(c).zip...
相位差(phase difference)是两个同频率正弦量之间相位的差异,公式为φ = (ωt + θu) - (ωt + θi) = θu - θi。如果φ > 0,那么u相对于i超前φ角;相反,如果φ ,i则超前uφ角。相位差的绝对值|φ|通常限定...
标题中的“the difference of set.rar_The Show_set”暗示了我们即将探讨的是关于集合(set)数据结构在不同场景下的差异或特性。集合是编程语言中常见的一种数据结构,它包含一组唯一的元素,通常不考虑元素顺序。...
《理解有限差分时域法》是一本关于FDTD(Finite-Difference Time-Domain,有限差分时域法)的详细教学资料。FDTD是一种通过在时域中直接对麦克斯韦方程进行数值求解来模拟电磁场问题的数值计算技术。它是一种广泛...
DoG (Difference of Gaussian)实现角点检测。 效果见http://blog.csdn.net/abcjennifer/article/details/7639488#comments
**高斯差分(Difference of Gaussian, DoG)详解** 高斯差分是计算机视觉和图像处理领域中一种常见的边缘检测算法,它基于数学形态学的理论,利用高斯滤波器的特性来识别图像中的边缘。在VS2008集成开发环境和...
igs观测数据文件处理,差分计算(c1-l1 c1-p1 c1-p2)
【新人教版Unit 1 Making a difference】是一个教学材料,主要围绕着科学界的重要人物以及一个关于成为天才的讨论话题展开。在这个单元中,学生将接触到一些著名的科学家,了解他们的成就,并探讨如果普通人能通过...
Google Earth Engine训练教程
JWT(JSON Web Token)是一种开放的标准(RFC 7519),定义了一种紧凑的、自包含的方式来安全地在各方之间传输信息作为一个 JSON 对象。这个信息可以被验证和信任,因为它是数字签名的。JWT 可用于认证和授权,是...
这篇文档是关于2020版高中英语教材的一部分,具体是"单元素养评价(二)Unit 2 Making a difference",这部分内容主要涉及听力练习,旨在提高学生的英语听力理解能力。 1. **听力理解技巧**:文档中的五段对话展示...
广义D. H. Lehmer数及其$m$次幂的差,徐哲峰,,令$n>2$, $mge 2$为整数. 对任意整数$a$满足$1le ale n, (a,n)=1$,存在唯一整数$b$满足$1le ble n$使得$bequiv a^mpmod n$,将其定义为$(a)_n$. 如果$a$ 和$(a^m)_
Difference-main.cpp
or facilitating the delineation of anatomical structures and other regions of interest. We present herein a critical appraisal of the current status of semi-automated and automated methods for the ...
学姐分享的署期作业:凸程序的差分/差分凸程序专题difference of co nvex program 2014_DC Programming and DCA for General DC Programs_book. pdf pres_ introduction to dc programming . pdf 201 5_Sparse semi...
But the difference between a website that was put up in minutes and a rich, interactive, well design program that works in multiple devices is like night and day, and the world’s users have come to ...