IfLocked

public synchronized void myMethod() {
    boolean hasLock = false;
    Object o = new Object();

    // Determine if current thread has lock for o
    hasLock = Thread.holdsLock(o);     // false
    synchronized (o) {
        hasLock = Thread.holdsLock(o); // true
    }

    // Check if current thread has lock for current object
    hasLock = Thread.holdsLock(this);  // true
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.