java 线程中的 wait()和sleep()

wait() 方法是写在java.lang.Object类中的

(ps: notify()  notifyAll()也是在Object中定义的)

wait()源码注释:

Causes the current thread to wait until either another thread invokes the java.lang.Object.notify() method or the java.lang.Object.notifyAll() method for this object, or a specified amount of time has elapsed. 


使当前线程进入等待状态 直到被唤醒。
The current thread must own this object's monitor. 


当前线程自己拥有锁的权限
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens: 


线程会一直在等待 直到以下四种情况发生
1.Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened. 
2.Some other thread invokes the notifyAll method for this object. 
3.Some other thread interrupts thread T. 
The specified amount of real time has elapsed, more or less. If timeout is zero, 
4.however, then real time is not taken into consideration and the thread simply waits until notified. 





sleep()方法是定义在java.lang.Thread中 

sleep()源码注释:


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. The thread does not lose ownership of any monitors.
让当前线程休眠一定的时长,不会释放锁


wait()代码:

ps:如果 timeout 为零,则不考虑实际时间,在获得通知前该线程将一直等待  反之等待一定的时间 (秒)

 public final void wait() throws InterruptedException {
        wait(0);
    }
 public final void wait(long timeout, int nanos) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeout++;
        }

        wait(timeout);
    }
 public final void wait(long timeout, int nanos) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0) {
            timeout++;
        }

        wait(timeout);
    }

sleep() 代码:

public static native void sleep(long millis) throws InterruptedException;

//native :可以看出此方法是调用底层

public static void sleep(long millis, int nanos)
    throws InterruptedException {
        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
            millis++;
        }

        sleep(millis);
    }







猜你喜欢

转载自blog.csdn.net/Jone__/article/details/70739052