前提:Java中
线程的6种状态
>>NEW
>>RUNNABLE
>>BLOCKED
>>WAITING
>>TIMED_WAITING
>>TERMINATED
1> 代码执行方式上:
sleep方法为线程类Thread上的
静态方法:Thread.sleep(long)
而wait方是某个对象的方法:obj.wait(),obj.wait(long);
2> 是否持有lock上:
执行sleep后不会释放lock,即一直拥有monitors,而执行wait后会释放lock
3> 是否消耗CPU时钟:
执行sleep后会消耗cpu时钟,而执行wait后不消耗cpu时钟;
3> 线程状态上:
执行sleep(nms)后,线程状态为:TIMED_WAITING
执行wait后,分情况,如果参数带时间,则为TIMED_WAITING,否则,不带时间,为WAITING
***********************************
"GC Daemon" #16 daemon prio=2 os_prio=0 tid=0x0000000002db2000 nid=0x3083 in Object.wait() [0x00007f05122cd000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f7344178> (a sun.misc.GC$LatencyLock)
at sun.misc.GC$Daemon.run(GC.java:117)
- locked <0x00000000f7344178> (a sun.misc.GC$LatencyLock)
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x0000000002348800 nid=0x3077 in Object.wait() [0x00007f051c909000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f7346748> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000f7346748> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"idle_connection_reaper" #135 daemon prio=5 os_prio=0 tid=0x00007efbb80a3000 nid=0xf4 waiting on condition [0x00007efb434f0000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.aliyun.oss.common.comm.IdleConnectionReaper.run(IdleConnectionReaper.java:24)
****************************************
【温馨提示】
如果您觉得满意,可以选择支持下,您的支持是我最大的动力: