为什么让挥发性Java中的方法? [英] Why make a method volatile in java?

查看:124
本文介绍了为什么让挥发性Java中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个方法进行挥发性?如何制作方法动荡变化的方法的行为?

编辑:我做了一个toString()上的一类对象(Java反射)返回一个Method对象。返回的字符串有对方法名挥发性改性剂以及公众中的知名度和void返回类型。研究产生的挥发性房产仅供参考。这就是为什么我问这个问题。

在方法声明为:

 公共挥发性org.osmdroid.api.IGeoPoint org.osmdroid.views.MapView.getMapCenter()
 

code的反射法:

 公共静态类测试{
    公共静态无效showMethods(目标对象){
        类<> clazz所= target.getClass();
        对于(方法方法:clazz.getMethods()){
            如果(方法!= NULL){
                的System.out.println(method.toString());
            }
        }
    }
}
 

方法调用:

  Test.showMethods(图形页面);
 

解决方案

我强烈怀疑,你看到的是,该JLS定义了以下位字段实际上是一个副作用:

 公共静态最终诠释挥发分= 0x00000040;
 

和下位的方法:

 静态最终诠释桥= 0x00000040;
 

请注意,它们具有相同的值(同位具有不同的含义的方法和字段)。

如果你调用如 Modifier.toString(INT)没有,因为文档建议:

  

请注意,要执行这样的检查已知类型的实体,如构造函数或方法,先和的toString 与适当的面罩一样的说法从一个方法 constructorModifiers methodModifiers

,那么你会得到不恰当的输出(包括桥的方法,自动生成用于如协变返回类型,显示为动荡)。

至少在当前的OpenJDK Method.toString()过滤了这一点;如果你不是,也许你正在使用的是不同的或更旧的版本不正确地做到这一点的JDK。

Why would a method be made volatile? How does making a method volatile change the method's behavior?

Edit: I did a toString() on a Method object returned by a class object (Java Reflection). The return string had a volatile modifier against the method name along with public visibility and void return type. Research yielded only information on volatile for properties. This is why I asked this question.

The method declaration was:

public volatile org.osmdroid.api.IGeoPoint org.osmdroid.views.MapView.getMapCenter()

Code for the Reflection Method:

public static class Test {
    public static void showMethods(Object target) {
        Class<?> clazz = target.getClass();
        for (Method method : clazz.getMethods()) {
            if (method!=null) {
                System.out.println(method.toString());
            }
        }
    }
}

Method invocation:

Test.showMethods(mapView);

解决方案

I strongly suspect that what you're seeing is a side-effect of the fact that the JLS defines the following bit for fields:

public static final int VOLATILE = 0x00000040;

and the following bit for methods:

static final int BRIDGE = 0x00000040;

Note that they have the same value (the same bit has a different meaning for methods and fields).

If you call e.g. Modifier.toString(int) without, as the documentation suggests:

Note that to perform such checking for a known kind of entity, such as a constructor or method, first AND the argument of toString with the appropriate mask from a method like constructorModifiers or methodModifiers.

then you'll get inappropriate output (including bridge methods, autogenerated for e.g. covariant return type, showing up as 'volatile').

At least the current OpenJDK Method.toString() filters this out; if yours isn't, perhaps you're using a different or older version of the JDK which doesn't do this correctly.

这篇关于为什么让挥发性Java中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆