简体   繁体   English

在Java中使用同步块来一次阻止对多个方法的访问

[英]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? 所以我的问题是, A.class像全局锁一样起作用? Meaning will one thread executing method doSomething() be blocked while another thread is executing doSomethingElse() ? 意思是一个线程执行方法doSomething()会被阻塞,而另一个线程正在执行doSomethingElse()吗?

Thank you. 谢谢。

A.class act like a global lock? A.class像全局锁一样起作用吗?

Any synchronized block locks a particular object. 任何synchronized块都会锁定特定对象。 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锁定充当您正在其中运行的ClassLoader的“全局”锁定,因为Java保证每个加载器中只有一个A.class对象实例。 A.class is an object just like new A() is an object. A.class是一个对象,就像new A()是一个对象一样。

Locking on A.class is the same as locking on a static method: 锁定A.class与锁定静态方法相同:

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. 为了进行比较,当您锁定实例方法(而不是静态方法)时,它与锁定正在执行的A实例相同。 In other words this : 换句话说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 . 这是有关lockJava文档

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. 您的示例将从所有由同一类加载器加载的A类的所有实例中获取该锁的方法中锁定线程,因此,如果任何一个线程获取了阻止所有其他线程访问该对象上的那些方法中的任何一个的锁。或该类的任何其他对象。 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 . 它获得对象锁定,所有其他线程将被阻塞,直到current thread releases the lock为止。 As per java specification: 根据java规范:

A synchronized method acquires a monitor (§17.1) before it executes. 同步方法在执行之前先获取一个监视器(第17.1节)。 For a class (static) method, the monitor associated with the Class object for the method's class is used. 对于类(静态)方法,使用与该方法的类的Class对象关联的监视器。 For an instance method, the monitor associated with this (the object for which the method was invoked) is used 对于实例方法,使用与此相关联的监视器(为其调用方法的对象)

.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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