The difference between Java multithreaded sleep () and wait ()

Source: Internet
Author: User

Get in touch with some multithreaded stuff, or start with Java.

I believe that the friends who read this article already know the difference between the process and the thread, and also know why to use multithreading.

The main source of these two methods is that sleep is used for thread control, while wait is used for inter-thread communication, and the methods associated with the wait are notify and Notifyall.

Difference One:

Sleep is the thread class method, the thread is used to control its own process, such as having a thread to tell the time, every second to print out a moment, then I need to put a sleep in front of the print method to be executed every second. It's like an alarm clock.

Wait is a method of the object class that is used to communicate between threads, which causes the process that currently owns the lock on the object to wait until the other thread calls the Notify method, but you can also give him a time to wake up automatically. This method is mainly used to walk between different threads of scheduling.

Difference Two:

With regard to the release of the lock, it is assumed that you already know the concept of lock and its significance. Calling the Sleep method does not release the lock (the feeling is that the sleep method is not related to the lock because he is a thread used to manage his own methods, not involving thread communication)

Explained in JDK 7:

"public static void Sleep (long Millis)

Throws Interruptedexception
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, sub Ject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

Public final void Wait () throws Interruptedexception
Causes the current thread to wait until another thread invokes the Notify () method or the Notifyall () method for this Obje Ct. In other words, this method behaves exactly as if it simply performs the call Wait (0). The current thread must own this object ' s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object ' s Mon Itor to wake up either through a call to the Notify method the Notifyall method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. "
Calling the wait method frees the current thread's lock (in fact, the communication between the threads is managed by the object, and all the threads that manipulate an object are managed by its own wait method, as if the object is a TV, three people are three threads, then the TV remote is the lock, If now a takes the remote control, the television calls the Wait method, then a will hand over its own remote control, by the JVM virtual machine dispatch, the remote control should give to WHO. "I think of a fun example: If a takes the remote control, he can use his sleep every 10 minutes to tune a TV station, while the rest of his desk 10 minutes, the remote control is still in his hand ~"

Difference Three:

Use area

Because of the special meaning of the wait function, he should be placed in a synchronous statement block, which makes sense.

Note: All two methods need to throw an exception

The difference between Java multithreaded sleep () and wait ()

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.