The difference between Java Thread.Sleep (long) and object.wait ()/object.wait (long) (GO)

Source: Internet
Author: User

One, Thread.Sleep (long) differs from object.wait ()/object.wait (long)
where sleep (long) is somewhat similar to the Wait ()/wait (long) behavior, the main differences are as follows:
1.thread.sleep (long) is a static method that belongs to the Thread class. Its basic semantics is to cause the currently running thread to pause for some time. The implementation detail is to put the current thread in the ready thread queue until the sleep time expires before it can be dispatched to the execution thread (which cannot be scheduled for execution until the time expires). This method can be called in the Sychronized code block without releasing the object lock, or it may not be in the sychronized code block. The thread of this method must be the currently running thread (if the code is in a thread class, not necessarily the thread instance where the code is located), even if it is called by sleep (long) or call Thread.CurrentThread (). Sleep (Long).


2.object.wait () is a method that belongs to a class instance (object and its subclasses, and also the Class class instance). Its basic semantics is to keep the current thread waiting until it is notified that the default is This.wait (). The implementation details are placing the current thread in the blocking thread queue, registering the current thread as the listener of the specified object, and locking the lock releasing the specified object, and when notified by Notify/notifyall, re-strives for the specified object's lock and removes the current thread from the listener of the specified object. Puts the current thread from the blocking queue into the ready queue, waiting to be dispatched. This method must be in the Sychronized code block, and the locked object is consistent with the object waiting for the source of the notification. While the wait (long) method blocks when a ready queue is placed, the wait time expires or is notified to be dispatched, and the other is the same as the Wait () method.


Note: There is a basic understanding of thread execution, readiness, blocking of three state transitions, and scheduling of threads (operating system scheduler threads) and ready thread queues, blocking thread queues (actual implementations may be more complex, such as priority, scheduling policies, etc.). The Wait/notify/notifyall object Listener and notification event patterns for objects (including instances of class classes) are recognized, as well as locks on objects, lock queues (actual implementations may be more complex, such as priority, lock contention, etc.), Blocking thread Listening queue (notify only notifies a listener when the schedule is unknown, and there is an automatic wake-up, conditional wakeup).

http://blog.csdn.net/qingmingcom/article/details/6590967

The difference between Java Thread.Sleep (long) and object.wait ()/object.wait (long) (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.