Java:为什么这个线程在它已经拥有锁的时候等待?
问题描述:
这是一个来自转储的单线程,用于说明问题。没有其他线程持有锁0x00000007d7f78430但仍然等待。Java:为什么这个线程在它已经拥有锁的时候等待?
"main" prio=6 tid=0x000000000033b800 nid=0x2478 in Object.wait() [0x000000000257d000] java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d7f78430> (a org.osgi.util.tracker.ServiceTracker$AllTracked)
at org.osgi.util.tracker.ServiceTracker.waitForService(ServiceTracker.java:456)
- locked <0x00000007d7f78430> (a org.osgi.util.tracker.ServiceTracker$AllTracked)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:190)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:165)
at org.apache.camel.test.blueprint.CamelBlueprintTestSupport.createCamelContext(CamelBlueprintTestSupport.java:116)
at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:247)
at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:217)
at org.apache.camel.test.blueprint.CamelBlueprintTestSupport.setUp(CamelBlueprintTestSupport.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
答
看看在org.osgi.util.tracker.ServiceTracker.waitForService(long timeout)方法。
你会看到,它的目的是:
等待至少一个服务由该ServiceTracker的跟踪。
所以这不是锁争用 - 底层系统使用锁来等待东西 - 在大多数情况下这实际上是一个非常好的主意。
+0
当然!谢谢! – Colin 2013-03-15 17:00:27
张贴代码.. – Pragnani 2013-03-15 16:23:42
您必须等到... – ZhongYu 2013-03-15 16:25:27
@ zhong.j.yu之前,您必须等待锁定,但是一旦您等待显示器被释放。 – assylias 2013-03-15 16:28:49