简体   繁体   English

同步

[英]Synchronization

Say I have a class with synchronized methods. 说我有一个带有同步方法的类。 It's actually a class that uses Apache POI to write excel files. 它实际上是一个使用Apache POI编写excel文件的类。

public static synchronized List<CellData> createRow(final CellData... columns) {
        List<CellData> row = new ArrayList<CellData>();

        if (columns != null && columns.length > 0) {
            for (CellData column : columns) {
                row.add(column);
            }
        }
        return row; 
    }

public synchronized void writeFile() throws IOException {
       .................
}

This helper class resides in a jar (common library) and many programs use it at the same time. 该帮助程序类驻留在jar(公共库)中,并且许多程序同时使用它。

Do I really need to make this class synchronized since every other class that uses it creates an instance of that helper class? 因为使用该类的所有其他类都创建了该帮助器类的实例,所以我真的需要使该类同步吗?

the synchronized keyword do synchronization only between threads that call the method on the same instance of the object. sync关键字仅在调用同一对象实例上的方法的线程之间进行同步。

In the case you're describing, since each other class create their own instance, the synchronized serves absolutely no purpose, so you can safely remove it. 在您描述的情况下,由于每个其他类都创建了自己的实例,因此同步绝对没有任何作用,因此您可以放心地删除它。

I don't see any variables that are preceded by this. 我看不到任何前面的变量this. , so I can't detect any member variables that are shared in your code. ,因此我无法检测到您的代码中共享的任何成员变量。 It looks like your createRow() uses only parameters that are passed in and local member variables. 看起来您的createRow()仅使用传入的参数和局部成员变量。 If that's true, I'd conclude that you don't need to synchronize. 如果是这样,我得出的结论是您不需要同步。

You only need to synchronize the access of shared data if there's a possibility that you'll have more than thread accessing the same instance of your class at the same time. 只有在线程同时访问类的同一个实例的情况下,才需要同步共享数据的访问。

So the questions are: 所以问题是:

  1. Do your methods actually share the access to any instance variables? 您的方法是否实际上共享对任何实例变量的访问?

  2. Are you ever going to have more than one thread accessing one instance of your class at the same time? 您是否将要有多个线程同时访问您的类的一个实例?

    In the situation you described does not seem to be something that anyone that uses that library would want to do. 在这种情况下,使用该库的任何人似乎都不希望这样做。 You could simply just document that this implementation is not synchronized. 您可以只记录该实现未同步。

如果该方法不使用该类的任何成员字段(即,它仅使用局部变量),则您不需要任何同步

Few things to note here 这里没什么要注意的

  • classes are not synchronized. 类不同步。
  • Method or method blocks are synchronized. 方法或方法块已同步。 When we say synchronized it implies that only one thread will be executing the code in the method or in the synchronized block. 当我们说同步时,它意味着只有一个线程将在方法或同步块中执行代码。
  • Synchronization is needed only when a shared resource is read and modified by multiple threads. 仅当多个线程读取和修改共享资源时才需要同步。
  • If in a method all your variables are local only then you do not need synchronization. 如果在方法中所有变量仅是局部变量,则不需要同步。 If have a variable which is not local or a parameter which has some other reference and they can be read/modified by multiple threads then you should think about synchronizing that method. 如果具有非本地变量或具有其他引用的参数,并且可以由多个线程读取/修改它们,那么您应该考虑同步该方法。

You should synchronize your method in case of altering data which can be accessed from several threads. 如果更改了可以从多个线程访问的数据,则应该同步您的方法。

Your method (at post) manipulates with data which was passed as a parameter, so you have not to use synchronization. 您的方法(在发布时)使用作为参数传递的数据进行操作,因此您不必使用同步。

But if your static method will manipulate (alter) with public static data, it will require using of synchronization of method. 但是,如果您的静态方法要处理(更改)公共静态数据,则将需要使用方法的同步。

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

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