Java multithreaded sleep () and Wait () difference (turn)

Source: Internet
Author: User
Tags thread class

Difference One:

Sleep is the thread class method that threads use to control their own processes, such as having a thread to tell the time, printing out a moment in each second, and then I need to add a single call to the Print method to perform it every second. It's like an alarm clock.

Wait is a method of the object class, used for communication between threads, which causes the process that currently owns the object lock to wake up when the other thread calls the Notify method, but you can also specify a time for him to wake up automatically. This method is mainly used to take the scheduling between different threads.

Difference Two:

On the release of the lock, let's assume that you already know the concept of the lock and its meaning. Calling the Sleep method does not release the lock (its own feeling is that the sleep method is not related to the lock, because he is a thread used to manage his own method, not involving thread communication)
Calling the wait method frees the current thread's locks (in fact, the communication between threads is managed by objects). All the threads that manipulate an object are managed by their own wait method, as if the object is a TV set, three people are three threads, then the TV remote is the lock, If a is now holding the remote control, the TV calls the Wait method, then a will hand over their remote control, the JVM virtual machine scheduling, the remote control should be handed to WHO. "I think of a funny example: If a has a remote control, he can use his sleep every 10 minutes to tune the TV station, and in his 10 minutes of the rest of the console, the remote is still in his hand ~"

Difference Three:

Use area

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

Note: All two methods need to throw an exception

Personal opinion: There is a second difference between sleep and wait, caused me a query to the Java threading mechanism, has not yet seen the JDK source code (in fact, see, is the wood to understand), the thread of synchronization management, is not by the object in the scheduling, if the object in the scheduling, then JDK 1.5 The newly introduced reentrantlock mechanism is more worthy to be advocated than the synchronized keyword. Because he is more able to reflect such a mechanism. A lot of people can't understand the difference between wait and sleep, I think because of the influence of synchronized keyword. Of course, I still do not understand the Java thread specific implementation, stay in doubt and have time to continue to study it

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.