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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值