简体   繁体   English

来自不同类的同步语句

[英]Synchronized statements from different classes

I have two classes B and C that implements the interface A. In both classes exist a method with a statement that needs to be synchronized. 我有两个实现接口A的类B和C。在这两个类中都存在一个带有需要同步的语句的方法。 I have this: 我有这个:

    class B implements A {
         @Override
         public void method(){
           //some code here
           synchronized (this.getClass()) {
             //some code here
           }    
        }
    }



        class C implements A {
             @Override
             public void method(){
               //some code here
               synchronized (this.getClass()) {
                 //some code here
               }    
            }
        }   

I need to synchronize the statements in both classes but with this.getClass return different classes and it can execute at the same time. 我需要同步两个类中的语句,但是使用this.getClass返回不同的类,并且它可以同时执行。 How can I synchronized these two statement from different classes in a way that it will execute only once at the same time? 如何同步来自不同类的这两个语句,使其只能同时执行一次?

The most likely reason for needing to synchronize them is that they are going to access some common object. 需要同步它们的最可能的原因是它们将访问某些公共对象。 If so, synchronize on that object. 如果是这样,请对该对象进行同步。

If not, and if you control interface A, you can add: 如果不是,并且控制接口A,则可以添加:

public static final Object LOCK = new Object();

and have them both synchronize on A.LOCK. 并使它们都在A.LOCK上同步。

Synchronize on some other object that both implementations of method() have access to. method()两个实现都可以访问的其他对象同步。

class D {
    public static final Object SYNC_OBJ = new Object();
}

class B implements A {
     @Override
     public void method(){
       //some code here
       synchronized (D.SYNC_OBJ) {
         //some code here
       }    
    }
}

class C implements A {
     @Override
     public void method(){
       //some code here
       synchronized (D.SYNC_OBJ) {
         //some code here
       }    
    }
}

You could declare a shared monitor object in A , for example: 您可以在A声明一个共享监视器对象,例如:

static final Object MONITOR = new Object();

Then use synchronized(MONITOR) in B and C . 然后在BC使用synchronized(MONITOR)

You can easily modify your code without introducing a new class/variable to do exactly what you want (assuming you want to synchronize all implementations of A): 您可以轻松地修改代码,而无需引入新的类/变量来完全完成您想要的事情(假设您要同步A的所有实现):

synchronized (A.class) {
    // do something
}

This is not a good solution however. 但是,这不是一个好的解决方案。 The right way to do it is using a lock. 正确的方法是使用锁。

Synchronized block in java is synchronized on some objects. Java中的同步块在某些对象上是同步的。 synchronized blocks synchronized on same object reference only allows 1 thread at a time. 在同一对象引用上同步的同步块一次仅允许1个线程。 other will be blocked until it leaves. 其他将被阻止,直到它离开。 So use same object reference for both methods. 因此,两种方法都使用相同的对象引用。 https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

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

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