What is the difference between sleep () and wait () in Java?

Source: Internet
Author: User

(Online answer:sleep is a thread-classmethod that causes this thread to pause for a specified time and Run the opportunity to other threads.) But the monitoring state is still maintained, and then will be self-active recovery. Calling sleep does not release the object lock.

wait is a Object Method of the class that is called on this object . wait method causes the thread to discard the object lock and to wait for the lock pool to wait for the object, only to be issued for this object Notify method (or Notifyall only after this thread enters the object lock pool is ready to get the object lock into the running state.

Sleepis that the running thread is actively letting outCPU,CPUto run other threads,Sleepafter the specified time,CPUwill go back to this thread and continue to run down. Assuming that the current thread has entered a sync lock,Sleepmethod does not release the lock, even if the current thread uses theSleepmethod to give upCPU, but other threads that are blocked by the sync lock will not be able to run. waitrefers to a thread that has entered a sync lock. Let yourself temporarily make a sync lock so that other threads waiting for the lock can get a synchronous lock and run, and only other threads call theNotifyMethod (Notifydoes not release the lock. Just to tell the caller thatwaitthe thread of the method is able to participate in the competition to get the lock, but not immediately get the lock, because the lock is still in the hands of others. Others haven't released yet. AssumptionsNotifyThere is a lot of code behind the method that requires the code to run out before releasing the lock, which can benotfiymethod After adding a wait and some code to see the effect), callingwaitmethod will dismiss the thread of thewaitstatus and the program can get the lock again after continuing to run down. Forwaitthe explanation must cooperate with the sample code to explain, only then appears oneself really clear.

Package Com.huawei.interview;public class Multithread {public static void main (string[] args) {new Thread (new Thread1 ()). Start (); try {thread.sleep);} catch (Interruptedexception e) {e.printstacktrace ();} New Thread (New Thread2 ()). Start (); private static class Thread1 implements Runnable {@Overridepublic void run () {synchronized (multithread.class) {System.ou T.println ("Enter thread1 ..."); System.out.println ("Thread1 is Waiting"); try {MultiThread.class.wait ();
/*[release the lock in two ways, the first way is that the program naturally leaves the scope of the monitor, that is, out of the synchronizedkeyword jurisdiction of the Code range, Another way to do this is to invoke the wait method of the monitor object within the code that Synchronizedkeyword governs. Here, use the Wait method to release the lock.

*/} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread1 is going on ..."); System.out.println ("Thread1 is being over!");}

/* Due to the Thread1 and the following Thread2 internal run method to use the same object as the monitor, we cannot use this here because the this in Thread2 and this Thread1 is not the same object. We use Multithread.class, the byte-code object, which refers to the same object when the variable is referenced in the current virtual machine. */}}private Static class Thread2 implements Runnable {@Overridepublic void run () {synchronized (multithread.class) {Syste M.out.println ("Enter thread2 ..."); System.out.println ("Thread2 notify other thread can release wait status."); MultiThread.class.notify ();
/* Because the Notify method does not release the lock, the THREAD2 calls the following sleep method to rest for 10 milliseconds. But Thread1 still won't run. Because the THREAD2 does not release the lock, Thread1 cannot get the lock.

*/system.out.println ("Thread2 is sleeping ten millisecond ..."); try {thread.sleep (10);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread2 is going on ..."); System.out.println ("Thread2 is being over!");}}}



What is the difference between sleep () and wait () in Java?

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.