Java:为什么这个线程在它已经持有锁时会等待?

时间:2013-03-15 16:23:09

标签: java multithreading osgi

这是来自转储的单个线程,用于说明问题。没有其他线程持有锁0x00000007d7f78430但它还在等待。

"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)

1 个答案:

答案 0 :(得分:1)

查看org.osgi.util.tracker.ServiceTracker.waitForService(long timeout)方法。

您将看到它旨在:

  

等待此ServiceTracker至少跟踪一项服务。

所以这不是锁争用 - 底层系统使用锁来等待东西 - 在大多数情况下这实际上是个好主意。