Difference between Java multi-thread sleep () and wait ()

Source: Internet
Author: User

Start with Java if you are familiar with multiple threads.

Read this articleArticleAll of my friends know the differences between processes and threads, and why multithreading is required.

The main sources of these two methods are sleep, which is used for thread control, and wait is used for inter-thread communication. The methods supporting wait include policy and policyall.


Difference 1:

Sleep is a method of the thread class and is used by the thread to control its own processes. For example, there is a thread to report time, and a time is printed every second, then I need to add a sleep before the print method so that I can execute it every second. Just like an alarm.

Wait is an object-class method used for inter-thread communication. This method will cause the current process that owns the object lock to wake up when other threads call the notify method, however, you can also specify a time for him to automatically wake up. This method uses scheduling between different threads.

 Difference 2:

For the release of locks, we assume that you already know the concept and significance of locks.Calling the sleep method will not release the lock (it seems that the sleep method is inherently irrelevant to the lock, because it is a thread used to manage its own method and does not involve thread Communication)

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, subject 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 Notify () Method or Policyall () Method for this object. 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 monitor to wake up either through a call to Notify Method Policyall Method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution."
Calling the wait method will release the lock of the current thread (in fact, inter-thread communication is managed by objects. All threads that operate on an object are managed by this object through their own wait methods, it is like this object is a TV, and three people are three threads. The TV's remote control is the lock. If a is holding the remote control, the TV calls the wait method, then a handed over his remote control, scheduled by the JVM virtual machine, and handed over to the remote control .) [I think of a fun example: if a calls a TV station once every ten minutes using his sleep while a calls a station for 10 minutes, the remote control is still in his hand ~]


Difference 3:

Region

Because of the special meaning of the wait function, it should be placed in the synchronous statement block to make sense..

Note:An exception must be thrown for both methods.


My opinion: the second difference between sleep and wait has aroused a question about the Java thread mechanism. Currently, I have not read the source code of JDK, it is understandable), thread synchronization management, whether it is scheduled by the object, if it is scheduled by the object, then the new reentrantlock mechanism introduced by JDK 1.5 is more worthy of promotion than the synchronized keyword. Because it can better reflect such a mechanism. Many people cannot understand the difference between wait and sleep. I think it is because of the influence of the synchronized keyword. Of course, I still don't understand the specific implementation of Java threads. I will leave some questions and try again later.


This small topic seems to be a frequent interview question for senior students. Thank you for sharing your experience ~

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.