方法声明
java.lang.Thread public static void sleep(long millis)
throws InterruptedException
导致当前的执行线程睡眠(暂时停止执行)指定的毫秒数 取决于系统定时器和调度器的精度和准确度 这个线程不会放弃对于任何监视器(call it 锁)的所有权
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.
Params:
millis – the length of time to sleep in milliseconds
Throws:
IllegalArgumentException – if the value of millis is negative
InterruptedException – if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
方法声明
java.lang.Thread public static void sleep(long millis,
int nanos)
throws InterruptedException
这个方法同上个方法相比 会增加一个纳秒的参数
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.
Params:
millis – the length of time to sleep in milliseconds
nanos – 0-999999 additional nanoseconds to sleep
Throws:
IllegalArgumentException – if the value of millis is negative, or the value of nanos is not in the range 0-999999
InterruptedException – if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
public static void sleep(long millis, int nanos)
throws InterruptedException {
//等待的时间不能<0
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
//调整的纳秒参数不能<0或者是>999999
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
//调整的nanos>500000或者nanos!=0&&mills==0
millis自增一下
if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
millis++;
}
sleep(millis);
}
wait方法和sleep方法的区别:
sleep method 是用于暂停进程的执行几秒或者其他的时间我们想要指定的时间
但是一旦调用了wait method 线程将会处于等待状态不会自动的恢复直到调用了notify或者是notifyAll方法
sleep() is a method which is used to pause the process for few seconds or the time we want to. But in case of wait() method, thread goes in waiting state and it won’t come back automatically until we call the notify() or notifyAll().
wait及sleep方法的主要的不同是 wait会释放掉锁或者监视器 但是sleep在等待的过程中不会释放锁或者监视器 wait方法用于线程的内部通信 sleep方法通常用于运行时暂停执行
The major difference is that wait() releases the lock or monitor while sleep() doesn’t releases the lock or monitor while waiting. wait() is used for inter-thread communication while sleep() is used to introduce pause on execution, generally.
Thread.sleep 将当前的线程调整到非运行时状态一段时间,这个线程保持拥有对象锁的状态 如果当前的线程在一个同步代码块或者方法(没有线程可以进入这个方法或者代码块)如果其他的线程调用了intertupt() 将唤醒等待的线程。
Thread.sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt(). it will wake up the sleeping thread.
sleep时一个静态方法 意味着它可以总是影响当前的线程(执行sleep方法的线程),通常错误的认为调用t.sleep()(但是t不是当前正在执行的线程)即使这样,也将休眠的是当前线程,而不是t线程
While sleep() is a static method which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
未持有锁的线程直接调用wait()会发生什么情况?
public class MyTest1 {
public static void main(String[] args) throws InterruptedException {
Object object=new Object();
object.wait();
}
}
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at com.compass.spring_lecture.thread.MyTest1.main(MyTest1.java:10)
必须要在synchronized代码块中或者instance method、static method
在调用wait方法时,线程必须要持有对象的锁,当调用wait方法时,线程就会释放掉对象的锁 在调用Thread类的sleep方法时,线程时不会释放掉对象的锁
public class MyTest1 {
public static void main(String[] args) throws InterruptedException {
Object object = new Object();
synchronized (object) {
object.wait();
}
}
}
反编译MyTest.class 并简单的查看一下信息
C:\spring_lecture\target\classes\com\compass\spring_lecture\thread>javap -c MyTest1
public class com.compass.spring_lecture.thread.MyTest1 {
public com.compass.spring_lecture.thread.MyTest1();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.InterruptedException;
Code:
0: new #2 // class java/lang/Object
3: dup
4: invokespecial #1 // Method java/lang/Object."<init>":()V
7: astore_1
8: aload_1
9: dup
10: astore_2
11: monitorenter//进入 持有对象的锁 synchronized
12: aload_1
13: invokevirtual #3 // Method java/lang/Object.wait:()V
16: aload_2
17: monitorexit//正常退出
18: goto 26
21: astore_3
22: aload_2
23: monitorexit //异常退出
24: aload_3
25: athrow
26: return
Exception table:
from to target type
12 18 21 any
21 24 21 any
}
网友评论