Difference between sleep and wait in Java

Source: Internet
Author: User

The sleep (), suspend (), and resume () methods are not recommended. We recommend that you use wait (), Policy (), and policyall ().

The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads may be running and not scheduled to give up execution, unless
(A) The "Awake" thread has a higher priority.
(B) The Running thread is blocked for other reasons.
 
When wait () is a thread interaction, if the thread sends a wait () call to a synchronization object X, the thread will pause the execution and the called object will enter the waiting state, wait until the wake-up or wait time is reached.
When wait () is called, the thread will release the lock mark it occupies, so that other synchronized data in the thread's object can be used by other threads.
Waite () and Y () are called in the synchronized function or synchronized block because they operate on the object's "lock flag.If the call is performed in the non-synchronized function or the non-synchronized block, the illegalmonitorstateexception exception may occur at runtime although the compilation is successful.

 

 

 

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 (scope of use)

Synchronized (x ){

X. Sort y ()

// Or wait ()

}

4. Sleep must capture exceptions, while wait, policy, and policyall do not need to capture exceptions.

Additional reading:

Sleep and wait in the Java thread work together to stop the running of the current thread task, but they are different. First, let's look at the constructor in sleep.

Sleep (long millis) 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.

Sleep (long millis, int Nanos) causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

The sleep method is a method in the Thread class. It indicates that a thread is put into sleep state. after a certain period of time, the sleep method automatically wakes up and enters the runable State instead of the running state immediately, because the thread scheduling mechanism also takes time to resume the running of a thread, after a thread object calls the sleep method, it will not release all the Object locks it holds, so it will not affect the running of other process objects. However, during sleep, other objects may call its interrupt () to generate an interruptedexception exception. If your program does not catch this exception, the thread will terminate abnormally, enter the terminated state. If your program captures this exception, the program will continue to execute the catch statement block (may also have finally statement blocks) and future code.

Note that the sleep () method is a static method, that is, it is only valid for the current object, through T. sleep (): it is wrong to let t object into sleep. It will only make the current thread sleep rather than T thread.

Wait Method

Void wait (long timeout)

Causes the current thread to wait until either another thread invokes the specified y () method or the policyall () method for this object, or a specified amount of time has elapsed.

Void wait (long timeout, int Nanos)

Causes the current thread to wait until another thread invokes the same y () method or the policyall () method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

Wait is a member method of an object. Once an object calls the wait method, it must use the Y () and yyall () Methods to wake up the process; if a thread has a synchronization lock for one or more objects, after wait () is called, this thread will release all the synchronization resources it holds, this is not limited to the object that is called the wait () method. The wait () method may also be generated when other objects call the interrupt () method in the wait process.

Interruptedexception. The effect and processing method are the same as those of sleep ().

Append content:

Collection is an interface under java. util. It is the parent interface of various collection structures.

Collections is a Java. util class that contains various static methods related to set operations.

The Root Interface in the collection hierarchy. Collection represents a group of objects, also known as collection elements. Some collections allow repeated elements, while others do not. Some collections are ordered, while others are disordered. JDK does not provide any direct implementation of this interface: it provides more specific sub-interfaces (such as set and list. This interface is usually used to pass collections and operate these collections where the maximum universality is required.

Collections this class is composed of static methods that operate on the collection or return the collection. It contains the polymorphism algorithm operated on the collection, that is, the package, which returns the new collection supported by the specified collection and a few other contents. If the collection or Class Object provided by methods of this class is null, these methods will throw nullpointerexception.

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.