Rajesh Kumar
Rajesh Kumar

Reputation: 89

Can a thread acquire a lock on a instance of a class,when other thread is executing a static synchronized method of this class?

suppose we have

class A {
    void m1() {
        synchronized (this) {
        }
        blah blah blah
        synchronized (this) {
        }
    }
}

My doubt is while executing blah blah blah, the currently executing thread with object o release lock, at this time if other thread with object o acquires the lock, There will be deadlock. So how can we say that we should try to use synchronized block instead of synchronized method?

Suppose thread t1 executing static synchronized method which acquires a class level lock, can other thread acquire a lock of an instance of this class to execute other synchronized methods?

Upvotes: 0

Views: 884

Answers (2)

rgettman
rgettman

Reputation: 178263

While executing "blah blah blah", the currently executing thread doesn't own any locks. It is possible for another thread to acquire the lock on the object, but that won't cause deadlock. The first thread will simply block until the other thread releases the lock.

A deadlock occurs when thread A owns lock 1, but needs lock 2 while thread B owns lock 2 but needs lock 1. That isn't occurring in your situation.

A class level lock is a different lock than the lock on an object instance. So, a class level lock won't interfere with another thread executing other synchronized methods on an object.

Upvotes: 2

bmargulies
bmargulies

Reputation: 100050

Yes. Synchronization on an object is completely independent of synchronization on the Class<?> object for the object's class.

Upvotes: 2

Related Questions