How to check the monitors on one object?

Discussion in 'Java' started by jack, Jul 1, 2003.

  1. jack

    jack Guest

    For example

    synchronized(myObj) {
    ......
    }

    If I can't get myObj synchronized, how do I know which thread holding the lock?

    Thanks

    Jack
    jack, Jul 1, 2003
    #1
    1. Advertising

  2. jack

    Harald Hein Guest

    "jack" wrote:

    > If I can't get myObj synchronized, how do I know which thread
    > holding the lock?


    If you can't aquire the lock, your thread blocks on the synchronized,
    and you can't execute any "who is holding the lock instead" code in
    that particular thread.

    Since 1.4 Thread has a method to check if the Thread holds a lock.
    Using ThreadGroup you could try to find all threads in the VM and check
    for every thread before you run into the synchronized. BUT this doesn't
    make much sense, too. Since you can't do it under the synchronized
    lock, you end up with a small time gap between your test and the
    synchronized. During this time gap another thread might aquire the
    lock, so any pre-locking test is meaningless.

    Welcome to the wounderful world of multi-threading.

    And even if you would find out who holds the lock, what do you want to
    do with that other thread? There will be a reason why the other thread
    holds the lock. And you can't terminate the thread (the API is
    deprecated). You could try to interrupt the other thread which might
    result in all sorts of things, or nothing (only a status flag is set).

    If you need this for debugging purposes only, get a good debugger. If
    you can't get one that can list threads and locks, then check your VM.
    Most have some key combination that allows to get some dump of threads
    and locks out of the VM while it runs.

    HH
    Harald Hein, Jul 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Meh-Lit Kim
    Replies:
    1
    Views:
    769
    Chris Uppal
    Oct 8, 2003
  2. Alex Hunsley
    Replies:
    0
    Views:
    341
    Alex Hunsley
    Jan 23, 2004
  3. Shachar

    Monitoring monitors using JVMPI

    Shachar, Feb 6, 2005, in forum: Java
    Replies:
    2
    Views:
    428
    Chris Uppal
    Feb 7, 2005
  4. =?ISO-8859-1?Q?Andreas_R=F8sdal?=

    monitors in C?

    =?ISO-8859-1?Q?Andreas_R=F8sdal?=, Feb 6, 2004, in forum: C Programming
    Replies:
    2
    Views:
    584
    Mark McIntyre
    Feb 12, 2004
  5. ThomasH
    Replies:
    7
    Views:
    455
    ThomasH
    Aug 30, 2006
Loading...

Share This Page