JAVA sleep () & Wait ()

Source: Internet
Author: User
Tags thread class

For the Sleep () method, we first need to know that the method belongs to the thread class. The wait () method is part of the object class.

The sleep () method causes the program to pause execution for the specified time, giving up the CPU to that other thread, but his monitoring state remains, and when the specified time is up, it will automatically resume its running state.

During the call to the sleep () method, the thread does not release the object lock.

When the Wait () method is called, the thread discards the object lock, enters the waiting lock pool waiting for the object, and the thread only enters the object lock pool when the Notify () method is called for this object.

Gets the object lock into the running state.

What do you mean?

To give a few examples:

 1/** 2 * 3 */4 package com.b510.test; 5 6/** 7 * Differences between sleep () and wait () in Java 8 * @author hongten 9 * @date 2013-12-1010 */11 public class TestD {pub Lic static void Main (string[] args) {new Thread1 ()). Start (); try {THREAD.SL EEP (Exception e) {e.printstacktrace ();}20 new Thread (New Threa  D2 ()). Start ();}22 the private static class Thread1 implements RUNNABLE{24 @Override25 public    void Run () {synchronized (Testd.class) {System.out.println ("Enter thread1 ..."); System.out.println ("Thread1 is waiting ..."); try {30//Call the Wait () method, the thread discards the object lock and enters Wait for this object to lock the pool to TestD.class.wait (); (Exception e) {E.printstacktrac E ();}35 System.out.println ("Thread1 is going on ..."); System.out.priNtln ("THREAD1 is over!!!");         PNS}38}39}40 private static class Thread2 implements RUNNABLE{42 @Override43 public void Run () {synchronized (Testd.class) {System.out.println ("Enter Thread2. ); System.out.println ("Thread2 is sleep ..."); 47//Only after calling the Notify () method for this object does this thread enter the object lock pool preparation Gets the object lock into the running state. TestD.class.notify (); 49//==================50//Difference 51/ /If we put the code: TestD.class.notify () and commented out, that is, Testd.class calls the Wait () method, but does not call the Notify () 52//method, the thread is always in the suspended state. The try {//sleep () method causes the program to pause execution for the specified time, yielding the CPU to that other thread, 55//But his monitoring status remains , the run state is automatically restored when the specified time is up. 56//During the call to the sleep () method, the thread does not release the object lock. Thread.Sleep (Exception e) {$ e.printstacktra CE ();}61 SystEm.out.println ("Thread2 is going on ..."); System.out.println ("THREAD2 is over!!!"); 63}64}65}66}

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!! Thread1 is going on .... THREAD1 IS-over!!!

If the code is commented out:

1 TestD.class.notify ();

Operating effect:

Enter Thread1...thread1 is Waiting...enter thread2....thread2 are sleep....thread2 is going on....thread2 are over!!!

And the program is always in a pending state.

① These two methods come from different classes, namely, sleep comes from the thread class, and wait comes from the object class.

Sleep is the static class method of thread, who calls who goes to sleep, even if you call B's Sleep method in a thread, actually a goes to sleep, to let the B thread sleep to call sleep in B's code.

② Lock: The most important thing is that the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method.

Sleep does not sell system resources; Wait is the thread waiting for the pool to wait, to assign system resources, and other threads to consume the CPU. The general wait does not add a time limit, because if the wait thread runs out of resources, it is useless to wait for all threads in the Notify/notifyall wake-up waiting pool to be called by other threads before it enters the ready queue to wait for the OS to allocate system resources. Sleep (milliseconds) can be specified by time to wake it up automatically, if the time is less than the interrupt () force interrupt.

The role of Thread.Sleep (0) is "triggering the operating system to immediately re-compete with the CPU".

③ Use range: Wait,notify and notifyall can only be used in synchronous control methods or synchronization control blocks, and sleep can be used anywhere.

Synchronized (x) {
X.notify ()
or wait ()
}

JAVA sleep () & Wait ()

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.