线程中sleep()和wait()的区别 | 您所在的位置:网站首页 › sleep和wait的区别mfc › 线程中sleep()和wait()的区别 |
sleep()和wait()的区别
sleep()
sleep()方法是Thread的静态方法,提供了两种sleep的方式可让我们更灵活的控制,目的是为了使线程睡眠一段时间,自然醒后继续执行,不存在继续竞争,因为期间并没有释放同步锁. public static native void sleep(long millis) throws InterruptedException; 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); } wait()来看源码,我们可以看见wait()是Object |
CopyRight 2018-2019 实验室设备网 版权所有 |