Differences among wait, sleep, policy, and policyall: policypolicyall

Source: Internet
Author: User

Differences among wait, sleep, policy, and policyall: policypolicyall
Threadexceptionobjectjavawait causes the current thread to wait until other threads call the notify or yyall methods of this object. The current thread must own this object monitor. This thread releases ownership of this monitor and waits until other threads wake up on the monitor of this object by calling the notify method or the notifyAll method. Then the thread will not continue execution until it obtains ownership of the monitor again.SleepSleep the currently running thread (paused) within a specified millisecond ). This thread does not lose the ownership of any monitor.Wait and sleep

Wait is a method of the Object class, and its range is the thread that stores the Object instance.

Sleep () is a static method exclusive to the Thread class, targeting a specific Thread.

 

The Wait method suspends the execution of the thread in which the object is located, so that the object enters the waiting state until the waiting time of the notify method notification or wait is reached. The Sleep method suspends the running of the thread, so that the thread enters the sleep state until the interrupt method is used to interrupt its Sleep or sleep. When the Wait method enters the waiting state, the synchronization lock is released (the lock object in the above example), while the Sleep method does not release the synchronization lock. Therefore, when no one goes to interrupt when a thread is infinitely Sleep, the program will cause a lot of trouble. notify is used to notify the thread, but the thread needs to get the lock before notify. It must be written in synchronized (lockobj. Wait also looks like this. A thread needs to release a lock and can only release it when it gets the lock. Therefore, wait also needs to be placed in synchronized (lockobj.

 

Sleep and interrupt

Interrupt is a very violent method. when you interrupt the Sleep of a thread, you do not need to get the lock of the thread. Although violent, it is also useful. When a thread has no time limit to sleep, only interrupt can wake him up. Interrupt will throw InterruptedException. This Exception is automatically thrown by the Thread class. Therefore, Interrupt has a strong blocking effect.

 

Wait and interrupt

Interrupt can interrupt wait. Unlike sleep interruption, the interrupted wait thread will not throw InterruptedException until it gets the lock again.

 

Resume and suspend have been abandoned by Java because they are inherently causing thread deadlocks.

Suspend is a greedy guy. When a thread is in suspend, the thread stops, but still holds the lock obtained before that. Other threads cannot use any resources they lock, unless the suspended thread is resume before it continues to run. For thread synchronization, it is much safer to use wait and policy.

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.