简体   繁体   English

获取锁对象线程正在等待

[英]get lock object thread is waiting on

How could I get lock object Thread is waiting on? 我怎样才能得到锁对象线程正在等待?

I have thread dump and I can see one SwingWorker started a confirmation dialog using invokeAndWait() awaiting user interaction. 我有线程转储,我可以看到一个SwingWorker使用invokeAndWait()启动了一个确认对话框,等待用户交互。
How could I detect this scenario programmatically? 如何以编程方式检测到这种情况?

    "Some swing worker -1-thread-8", WAITING, prio=5, tid=128
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:485)
        at java.awt.EventQueue.invokeAndWait(EventQueue.java:1038)
        at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1326)
        at ...
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

I posted a similar answer a while back . 不久前,我也发布了类似的答案 The trick is to use Management extensions provided by the JDK in case you are interested in the status of all the running threads. 技巧是使用JDK提供的管理扩展,以防您对所有正在运行的线程的状态感兴趣。

See the Thread.holdsLock(Object) method. 请参见Thread.holdsLock(Object)方法。

AFAICT this is the only way to determine if a thread holds a lock on a particular object; AFAICT这是确定线程是否在特定对象上持有锁的唯一方法。 I don't think there is a way to programmatically access the object on which a thread is waiting without knowing which one(s) it might be ahead of time. 我认为没有一种方法可以以编程方式访问线程正在等待的对象,而又不知道它可能提前到哪个对象。

Basiclly a lock has a list of the threads waiting on him, the lock here is an object's lock, therefore it's a Monitor object. 基本上,锁具有等待他的线程列表,此处的锁是对象的锁,因此它是Monitor对象。

even if you do get access to the list (which is unlikly,due to concurrency and security reasons) it would be almost impossible to guarantee you'll get the correct list at a specific time every time. 即使您确实可以访问该列表(由于并发和安全性原因,这很不巧),也几乎不可能保证每次都在特定时间获得正确的列表。

you may find the following printouts usefull,try getting the monitor's list by the thread using it and printing before releasing the lock(for example monitor's method -> See the getObservedObjects() method. ),and also make sure every thread writes his id before acquiring,both may result in an incorrect temporary values,but combined it would give you an indication to your needs. 您可能会发现以下打印输出很有用,请尝试使用该线程获取监视程序的列表并在释放锁定之前进行打印(例如,监视程序的方法->请参阅getObservedObjects()方法。),并确保每个线程在写入之前都输入了他的ID两者都可能会导致错误的临时值,但结合使用它们会给您指示您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM