What is the difference between sleep and wait?

Source: Internet
Author: User

The first explanation:

Similar functions are used for thread control. The biggest difference between them is that sleep () does not release the synchronization lock and wait () releases the synchronization contraction.

In addition, the difference in usage is that sleep (milliseconds) can automatically wake up with a specified time. If the time is not enough, you can only call interreput () to forcibly interrupt; wait () it can be directly invoked using Y.

The second explanation:

Sleep is a static method of the thread class. Sleep is used to set the time for the thread to sleep and resume when the time arrives. That is to say, sleep will resume thread execution when the time reaches the event. For example:

Try {
System. Out. println ("I'm going to bed ");
Thread. Sleep (1000 );
System. Out. println ("I wake up ");
}
Catch (intrruptedexception e ){
}

Wait is an object method, that is, you can call the wait Method for any object. Calling the wait method will suspend the caller's thread, the caller will not be re-activated until other threads call the notify method of the same object. For example:

// Thread 1

Try {
OBJ. Wait (); // suspend thread until obj. Y () is called
}
Catch (interrputedexception e ){
}

Third explanation:

There is a fundamental difference between the two.
Sleep () is used to suspend a thread for a period of time. Its control scope is determined by the current thread, that is, the thread is determined. for example, what I want to do is "ignition-> boil". When I finish the fire, I don't need to boil water immediately. I need to take a rest and then burn it again. the initiative in operation is controlled by my process.

And wait (), first of all, this is called by a certain object. This object is understood as a speaker. When this person says "Suspend!" In a thread! ", Which is also thisobj. wait (), whether the pause here is blocking, or "ignition-> boiling-> cooking", thisobj is like a person who monitors me standing next to me, originally, this thread should execute 1, execute 2, and then execute 3. Then, if the thread is paused by the object at 2, I will always wait here without executing 3, but the process is not over yet. I always wanted to cook, but I was not allowed until the object said "the thread for notifying the pause is started! ", That is, when thisobj. Y () is used, I can cook it. The paused thread will continue execution from the paused thread.

In fact, both of them can suspend the thread for a period of time, but the essential difference is the control of the running status of a thread and the communication between threads.

In the java. Lang. Thread class, sleep (),
The Java. Lang. object class provides the wait (), Y (), and notifyall () Methods to operate threads.
Sleep () can sleep a thread. The parameter can specify a time.
Wait () can suspend a thread until it times out or the thread is awakened.
Wait has two forms: Wait () and wait (milliseconds ).
Sleep and wait have the following differences:
1. These two methods come from different classes: thread and object.
2. The most important thing is that the sleep method does not release the lock, and the wait method releases the lock, so that other threads can use synchronous control blocks or methods.
3. Wait, policy, and policyall can only be used in synchronous control methods or synchronous control blocks, while sleep can be used in
Use anywhere
Synchronized (x ){
X. Sort y ()
// Or wait ()
}
4. Sleep must capture exceptions, while wait, policy, and policyall do not need to capture exceptions.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/liuzhenwen/archive/2009/05/20/4202967.aspx

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.