Why make a method volatile in java?

后端 未结 7 1200
逝去的感伤
逝去的感伤 2020-12-15 06:23

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 r

相关标签:
7条回答
  • 2020-12-15 06:55

    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.

    0 讨论(0)
提交回复
热议问题