一:sleep 和 wait
sleep()方法:
- 功能:让当前线程休眠指定时间,休眠时间的准确性依赖于系统时钟和CPU调度机制
- 是Thread类的静态方法
- 可在任何地方调用,需要处理InterruptedException
- 当前线程调用sleep()方法,当前线程会进入阻塞状态,sleep()结束进入可执行状态
- sleep()方法不释放已获取的锁资源
wait()方法:
- 让当前线程进入等待状态,当别的其他线程调用notify()或者notifyAll()方法时,当前线程进入可运行状态
- 是Object类的成员方法
- wait方法必须在同步上下文中调用,例如:同步方法块或者同步方法中,这也就意味着如果你想要调用wait方法,前提是必须获取对象上的锁资源
- 当wait方法调用时,当前线程将会释放已获取的对象锁资源,并进入等待队列,其他线程就可以尝试获取对象上的锁资源
sleep() vs wait()
sleep | wait | |
---|---|---|
同步 | 不需要在同步方法或同步块中调用 | 只能在同步上下文中调用wait方法,否则或抛出IllegalMonitorStateException异常 |
作用对象 | 定义在java.lang.Thread中,作用于当前线程 | 定义在Object类中,作用于对象本身 |
释放锁资源 | 否 | 是 |
唤醒条件 | 超时或者调用interrupt()方法 | 其他线程调用对象的notify()或者notifyAll()方法 |
方法属性 | sleep是静态方法 | wait是实例方法 |
二:方法签名:
sleep()方法是Thread类的静态方法
/*** 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.** @param millis* the length of time to sleep in milliseconds** @throws IllegalArgumentException* if the value of {@code millis} is negative** @throws InterruptedException* if any thread has interrupted the current thread. The* <i>interrupted status</i> of the current thread is* cleared when this exception is thrown.*/public static native void sleep(long millis) throws InterruptedException;
wait()方法是Object类的成员方法
/*** Causes the current thread to wait until another thread invokes the* {@link java.lang.Object#notify()} method or the* {@link java.lang.Object#notifyAll()} method for this object.* In other words, this method behaves exactly as if it simply* performs the call {@code wait(0)}.* <p>* The current thread must own this object's monitor. The thread* releases ownership of this monitor and waits until another thread* notifies threads waiting on this object's monitor to wake up* either through a call to the {@code notify} method or the* {@code notifyAll} method. The thread then waits until it can* re-obtain ownership of the monitor and resumes execution.* <p>* As in the one argument version, interrupts and spurious wakeups are* possible, and this method should always be used in a loop:* <pre>* synchronized (obj) {* while (<condition does not hold>)* obj.wait();* ... // Perform action appropriate to condition* }* </pre>* This method should only be called by a thread that is the owner* of this object's monitor. See the {@code notify} method for a* description of the ways in which a thread can become the owner of* a monitor.** @throws IllegalMonitorStateException if the current thread is not* the owner of the object's monitor.* @throws InterruptedException if any thread interrupted the* current thread before or while the current thread* was waiting for a notification. The <i>interrupted* status</i> of the current thread is cleared when* this exception is thrown.* @see java.lang.Object#notify()* @see java.lang.Object#notifyAll()*/public final void wait() throws InterruptedException {wait(0);}