Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Local Object Reference escaping

Local Object Reference escaping

Scheduled Pinned Locked Moved Java
javaquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neo10101
    wrote on last edited by
    #1

    In terms of thread safety: How can it be determined an object escapes a method call, without using JVM escape analysis? ('Escaping' means that an object can be accessed after the method completes) A couple of examples on how an object can escape a method call: 1) The method returns the object that was locally created:

    public MyObject someMethod(){
    MyObject localObject = new MyObject();
    return localObject();
    }

    //Another thread can now mutate this object because it has a reference to it:
    someMethod.changeSomething();

    1. One of the methods called with the LocalObject as parameter, stores the LocalObject instance in a way that allows access to it from other threads.

        MyMember member;          
      
        public void someMethod(){  
            LocalObject localObject = new LocalObject();  
      
            localObject.callMethod();  
            method2(localObject);  
        }  
      
        public void method2(LocalObject localObject){  
            localObject.setValue("value");  
            member = localObject;  
        }  
      

    Does anyone know any more practical examples on how an object can escape a method call?

    J 1 Reply Last reply
    0
    • N Neo10101

      In terms of thread safety: How can it be determined an object escapes a method call, without using JVM escape analysis? ('Escaping' means that an object can be accessed after the method completes) A couple of examples on how an object can escape a method call: 1) The method returns the object that was locally created:

      public MyObject someMethod(){
      MyObject localObject = new MyObject();
      return localObject();
      }

      //Another thread can now mutate this object because it has a reference to it:
      someMethod.changeSomething();

      1. One of the methods called with the LocalObject as parameter, stores the LocalObject instance in a way that allows access to it from other threads.

          MyMember member;          
        
          public void someMethod(){  
              LocalObject localObject = new LocalObject();  
        
              localObject.callMethod();  
              method2(localObject);  
          }  
        
          public void method2(LocalObject localObject){  
              localObject.setValue("value");  
              member = localObject;  
          }  
        

      Does anyone know any more practical examples on how an object can escape a method call?

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Neo10101 wrote:

      //Another thread can now mutate this object because it has a reference to it:

      No it can't. Not from that method. Thread 1 calls that method. That method create a brand spanking new object and returns it to the caller. Thread 2 calls that method. That method create a brand spanking new object and returns it to the caller. Neither object is the same. The calling sequence of the threads 1 and 2 are irrelevant (again in the context of this method only) each will still get a new object which is untouchable by the other thread.

      Neo10101 wrote:

      Does anyone know any more practical examples on how an object can escape a method call?

      The method returns a data member of the class which is an object. So public MyObject getMyObject() { return myObject; }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups