简体   繁体   English

同步方法和静态变量访问JAVA / ANDROID

[英]Synchronized methods and static variable access JAVA / ANDROID

I need help with some JAVA theory... 我需要一些JAVA理论的帮助......

So I recently discovered that if an app uses multiple threads, and there is any possibility of different threads accessing the same shared variable at the same time, then one should use "synchronized" methods to get/set said variable. 所以我最近发现,如果一个应用程序使用多个线程,并且不同线程有可能同时访问同一个共享变量,那么应该使用“synchronized”方法来获取/设置所述变量。

So... in my (location aware) app, I have variables LAT, and LON that belong to the MainActivity and are static. 所以...在我的(位置感知)应用程序中,我有变量LAT和属于MainActivity的LON并且是静态的。 They are accessed from background services like so: 可以从后台服务访问它们,如下所示:

appendToPOST(MainActivity.LAT);

And in MainActivity itself, I am using google play location services, and so in the onLocationChanged() callback method in MainActivity, I have: 在MainActivity本身,我正在使用谷歌播放位置服务,因此在MainActivity中的onLocationChanged()回调方法中,我有:

LAT = [arbitrary Double value goes here];

So therefore, that means I should implement: 因此,这意味着我应该实施:

public static synchronized void setLAT(Double inLAT){

    LAT = inLAT;

}

public static synchronized void setLON(Double inLON){

    LON = inLON;

}

public static synchronized Double getLAT(){

    return LAT;

}

public static synchronized Double getLON(){

    return LON;

}

Correct? 正确? Thanks for any clarification/help. 感谢您的任何澄清/帮助。

Added: Also, should I change the code in the onLocationChanged() method to use the synchronized methods, even though it exists in the same class as the variable? 补充:另外,我是否应该更改onLocationChanged()方法中的代码以使用synchronized方法,即使它与变量存在于同一个类中?

First, synchronized static methods are type of Java's built-in locks. 首先, synchronized static methods是Java内置锁的类型。 They use class object as a lock, so only one thread at a time can work with the class. 它们使用类对象作为锁,因此一次只有一个线程可以使用该类。 If one thread executing one of the methods, other threads cannot execute any of the objects methods. 如果一个线程执行其中一个方法,则其他线程无法执行任何对象方法。 This is locking type of synchronization. 这是锁定类型的同步。 It affects performance. 它会影响性能。 But if high performance is not required, this solution will work. 但如果不需要高性能,此解决方案将起作用。 You need mark methods that read/change data with synchronized keyword. 您需要使用synchronized关键字读取/更改数据的标记方法。

Second, I assume your variables mean coordinates of location: latitude and longitude. 其次,我假设你的变量意味着位置坐标:纬度和经度。 If so, they must be accessed together. 如果是这样,则必须一起访问它们。 You can make new composite class. 您可以创建新的复合类。 If they are not accessed at the same time, it can lead to race condition. 如果不同时访问它们,则可能导致竞争状况。

PS: let us know if synchronization affects performance. PS:如果同步影响性能,请告诉我们。 There are non-locking at a class level solutions. 在类级解决方案中存在非锁定。

Your change is correct so far. 到目前为止,您的更改是正确的 And as you already mentioned every access (local access as well) to these shared state variables must be using the synchronized accessors. 正如您已经提到的,对这些共享状态变量的每次访问(本地访问)都必须使用同步访问器。

Furthermore, if the values of LAT and LON belong together (I assume you are representing a location with these values) you should make sure they can not be modified or retrieved independent of each other. 此外,如果LATLON的值属于一起(我假设您表示具有这些值的位置),则应确保它们不能彼此独立地修改或检索。 That is you should not add a setter and getter for each value but one for both. 那就是你不应该为每个值添加一个setter和getter,而是为两者添加一个。 And if these values belong together you could think of introducing a new class that represents both values like 如果这些值属于一起,你可以考虑引入一个代表两个值的新类

public class Location {
    private double mLon;
    private double mLat;

    // Add getters and setters. These don't need to be synchronized
}

and change the synchronized accessors in MainActivity to 并将MainActivity的同步访问器更改为

public static synchronized setLocation(final Location inLocation) {
    ...
}

Just for completeness using synchronized blocks is probably the most common but not the most efficient solution for implementing thread synchronization on shared state objects. 为了完整性,使用synchronized块可能是在共享状态对象上实现线程同步的最常见但不是最有效的解决方案。 If it comes to performance you should probably take a look at the classes in java.util.concurrent . 如果涉及到性能,你应该看一下java.util.concurrent的类。 See here for a short introduction of the Lock object. 请参阅此处以获取Lock对象的简短介绍。

UPDATE : 更新
Updated answer with code example 使用代码示例更新了答案

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

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