Why make a method volatile in java?

后端 未结 7 1199
逝去的感伤
逝去的感伤 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:29

    I did some research and my conclusions closely mirror Cowan's. The following code produces the correct output on a Sun JDK 1.6_13

    public void go()
    {
    
        try
        {
            Class<?> forName = Class.forName("org.osmdroid.views.MapView");
            Method[] methods = forName.getMethods();
            for(Method m : methods)
            {
                String name = m.getName();
                if(name.equals("getMapCenter"))
                {
                    System.out.println(m);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    

    Output

    public org.osmdroid.api.IGeoPoint org.osmdroid.views.MapView.getMapCenter()
    public org.osmdroid.util.GeoPoint org.osmdroid.views.MapView.getMapCenter()
    

    What JDK are you using and what version is it ? The logic used to construct the Method's toString() would be interesting to peruse.

    Those interested in the android jar can download it from here

    http://code.google.com/p/osmdroid/downloads/list http://www.jarvana.com/jarvana/archive-details/com/google/android/android/1.5_r3/android-1.5_r3.jar

    0 讨论(0)
  • 2020-12-15 06:33

    It seems that "volatile" methods are produced under the hood by the Java compiler, as "glue methods" for generic methods. Generics will produce under the hood methods which will accept only Object as parameters, and cast them to the specific generic type, while these methods are invisible to the developer. There is a safer way to determine these methods though, by using the method.isBridge()

    0 讨论(0)
  • 2020-12-15 06:41

    You can't. volatile is only a valid modifier for a field.

    0 讨论(0)
  • 2020-12-15 06:41

    First of all, no volatile methods in Java. Full stop.

    Java allows to declare fields as volatile. The Java language spec explains the purpose:

    A field may be declared volatile, in which case the Java memory model ensures that all threads see a consistent value for the variable.

    Now, if we try to translate it to methods: in case of a volatile method something would ensure, that all threads see a consistent byte code for the method. But that is guaranteed anyway. Threads don't see different versions of a class, they all see the same (compiled) byte code (as long as they don't play tricks with classloaders...).

    So there is no need for volatile methods in Java.

    0 讨论(0)
  • 2020-12-15 06:49

    have you read

    http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html


    just to make the answer complete :

    as everybody else is pointing out : you can't make a method volatile.

    0 讨论(0)
  • 2020-12-15 06:51

    You can't make a method volatile. It won't compile.

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