1. 程式人生 > >java 執行緒中的 wait()和sleep()

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);
    }