Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20966

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

Upvotes: 0

Views: 1454

Answers (3)

shaydel
shaydel

Reputation: 589

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.

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.

Upvotes: 1

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

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.

Upvotes: 3

maerics
maerics

Reputation: 156424

See the Thread.holdsLock(Object) method.

AFAICT this is the only way to determine if a thread holds a lock on a particular object; 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.

Upvotes: 1

Related Questions