简体   繁体   English

Java同步方法-OCPJP

[英]Java Synchronized Methods - OCPJP

I'm reading a book regarding to the OCPJP exam. 我正在读一本有关OCPJP考试的书。 It says 它说

An entire method can be declared synchronized. 可以将整个方法声明为已同步。 In that case, when the method declared as synchronized is called, a lock is obtained on the object on which the method is called, and it is releases when the method returns to the caller. 在那种情况下,当调用声明为“同步”的方法时,将在调用该方法的对象上获得一个锁,并在方法返回到调用方时释放。

What I got from this phrase; 我从这句话中得到了什么;

If there is a class called A, which calls a synchronized method, resides in the class B, is acquiring the lock from an object of class A (which the method is called). 如果存在一个名为A的类,该类调用一个同步方法,则该类驻留在类B中,该类正在从类A的对象(称为方法)获取锁。

Is it correct or not? 是对的吗?

Does it need to acquire the lock from an object of class B? 是否需要从B类的对象获取锁? Just like synchronized block using this reference. 就像使用此参考的同步块一样。

If there is a class called A, which calls a synchronized method, resides in the class B, is acquiring the lock from an object of class A (which the method is called). 如果存在一个名为A的类,该类调用一个同步方法,则该类驻留在类B中,该类正在从类A的对象(称为方法)获取锁。

No, in this case object of class B will be used as lock, since the method belongs to Class B 不,在这种情况下,类B的对象将用作锁,因为该方法属于类B

The lock is on the B-object that is used by A. This object can be anywhere, in my example it is in class A, but it could be passed to A through a parameter of the execute()-method. 锁位于A使用的B对象上。此对象可以在任何位置,在我的示例中为A类,但可以通过execute()方法的参数将其传递给A。

Imagine this: 想象一下:

public class A{
    public B objectB;

    public void execute(){
        objectB = new B();

        // objectB is synchronized during the execution of the following call, no other        
        // Thread can access ANY synchronized method of objectB or any 
        // synchonized(this) block within objectB in this time
        objectB.syncedMethod();   
    }
}

public class B{
    public synchronized void syncedMethod(){
        //doImportantStuff
    }

    public synchronized void anotherSyncmethod(){
        //do other important stuff
    }
}

The effect is same like synchronizing on "this" inside the method (I guess the resulting Java bytecode will be different, see comments) 效果就像在方法内部同步“ this”一样(我想生成的Java字节码将有所不同,请参见注释)

public class B{
    public void syncedMethod(){
      synchronized(this){
        //doImportantStuff
      }
    }

    public void anotherSyncmethod(){
        synchronized(this){
         //do other important stuff
        }
    }
}

在此处输入图片说明

Every object has a lock associated with it and all of its synchronized methods have to acquire its lock before enter the method. 每个对象都有一个与之关联的锁,并且其所有同步方法都必须在进入该方法之前获取其锁。 So, if any thread calls m1() or m2() on the object depicted in the diagram above, it has to acquire the lock on the object which contains m1() or m2() and not on itself. 因此,如果有任何线程在上图中描述的对象上调用m1()或m2(),则它必须获取包含m1()或m2()而不是自身的对象上的锁。

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

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