• Post Reply Bookmark Topic Watch Topic
  • New Topic

Local object references  RSS feed

 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to here: http://tutorials.jenkov.com/java-concurrency/thread-safety.html 'Local Object References' section.

I am trying to see if I understand correctly what is written there. So, we have a few scenarios:

1) "If an object created locally never escapes the method it was created in, it is thread safe". What is considered 'escaping'?

2) "The LocalObject instance in this example is not returned from the method, nor is it passed to any other objects that are accessible from outside the someMethod() method"
not thread safe:



3) "The only exception is of course, if 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."
not thread safe:


Am I understanding these concepts correctly?
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:
1) "If an object created locally never escapes the method it was created in, it is thread safe". What is considered 'escaping'?


There is a feature in the JVM (I believe that it is still optional) that does escape analysis. So, what is escape analysis on a method?

To escape a method call, it means that an object can be accessed after the method completes. Now, obviously, the local variables are out of scope upon completion, this analysis is in regards to the instances that were created by the method. And as you mentioned, it can get really complex -- as there are a lot of ways that it can happen.

Anyway, in terms of threads, if an instance is deemed to not be able to escape a method, then there is no reason to honor synchronization. There is no need to synchronize, if it is not to be used by multiple threads. And there is no way that it can be used by multiple threads, if it can't escape the method.

Henry

 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:
3) "The only exception is of course, if 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."
not thread safe:



It is a bit more complex than that. Yes, at line 7, since the instance is passed to another method, escape analysis must be done on that called method to make sure that the parameter can't escape -- and if it can, then it is considered an escape of the calling method.

However, that is not the only method. Escape analysis also needs to check the method calls at line 6 and method calls at line 11 -- quite frankly, it checks all of the methods of the LocalObject class type. Keep in mine that an instance has access to itself via the "this" reference, so using an instance can cause an escape.

Henry
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: Keep in mind that an instance has access to itself via the "this" reference, so using an instance can cause an escape.

I don't quite understand this. May I see a code snippet of this scenario please?
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:
Henry Wong wrote: Keep in mind that an instance has access to itself via the "this" reference, so using an instance can cause an escape.

I don't quite understand this. May I see a code snippet of this scenario please?


For example, if the LocalObject class did this...



Then after calling the callMethod() method, previous example line 6, the local object is now accessible by any thread via the static member of the LocalObject class. In other words, it escape the method.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!