简体   繁体   中英

Using synchronized block in java to block access to multiple methods at once

if I have two methods:

public class A {

    public void doSomething() {
        synchronized(A.class) {
            //synchronized block here
        }
    }

    public void doSomethingElse() {
        synchronized(A.class) {
           //synchronized block here
        }
    }
}

So my question is, does the A.class act like a global lock? Meaning will one thread executing method doSomething() be blocked while another thread is executing doSomethingElse() ?

Thank you.

A.class act like a global lock?

Any synchronized block locks a particular object. Locking on A.class acts as a "global" lock for the ClassLoader that you are running in, because Java guarantees that there is only one instance of A.class object in each loader. A.class is an object just like new A() is an object.

Locking on A.class is the same as locking on a static method:

public class A {
    public static synchronized void someStaticMethod() {
        // in here you are locked on A.class as well
    }
    public void doSomething() {
        synchronized (A.class) {
            // this block is locked on the same object as someStaticMethod()
        }
    }
}

For comparison, when you lock on an instance method (as opposed to a static method), it is the same as locking on the instance of A that is being executed. In other words this :

public class A {
    public synchronized void someInstanceMethod() {
        // in here you are locked on the instance of A (this)
    }
    public void doSomething() {
        synchronized (this) {
            // this block is locked on the same instance of A
        }
    }
}

Again, it is about the particular object in question. Here's the Java documentation about locks .

Your example will lock threads out of all methods that acquire the lock in all instances of the A class that are loaded by the same classloader, so if any one thread acquires the lock that blocks all other threads from accessing any of those methods on that object or any other object of that class. Typically setting up something like this would likely be a bad idea. The lock will be unnecessarily coarse-grained (causing threads to wait even if they only want to access data belonging to different instances where there would be no sharing) and will restrict concurrency excessively. If you have a design that actually needs this you should question your design.

It gets object lock and all other threads will be blocked until until current thread releases the lock . As per java specification:

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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