• Post Reply Bookmark Topic Watch Topic
  • New Topic

local variable thread safe?  RSS feed

 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can two threads access the same method(non synchronized)on the same instance at the same time?If yes will the local variables in that method be thread safe?
Thanks in advance,
William Kane
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by william kane:
Can two threads access the same method(non synchronized)on the same instance at the same time?


Yes.


If yes will the local variables in that method be thread safe?


Yes, the variables themselves will be. If those variables point to shared objects, then of course those objects won't be. But local variables themselves are always thread-safe, since by definition each thread has its own copies.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To illustrate what that means this would be perfectly safe as each Thread will have it's own copy of i:



Whereas the following may lead to problems because despite the fact each Thread has it's own variable o they both point to the same shared resource:

 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
To illustrate what that means this would be perfectly safe as each Thread will have it's own copy of i:



Whereas the following may lead to problems because despite the fact each Thread has it's own variable o they both point to the same shared resource:



Thanks Ken,
Then will
Make the acess to anObject thread safe?

Also if the each thread has its own copy to local variable why does the first method(the one that access variable i) need to be synchronized?
Thanks again,
William Kane
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those variables point to shared objects, then of course those objects won't be.


Absolutely right. I'd go on to say or if you make those variables into shared objects in other threads they won't be safe.

Now I've given my local variable to two other threads that can wreak havoc at will.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then will

Make the acess to anObject thread safe?


No, I was using that as an example of something not thread-safe. Each thread has it's own copy of variables, but that doesn't mean they have their own copy of an Object. anObject is just a reference, if you alias o to that reference it's still pointing to the same Object. Invocations in multiple threads will still be on that same Object and thus unless the Object is thread-safe itself and designed for this you will have problems.

Also if the each thread has its own copy to local variable why does the first method(the one that access variable i) need to be synchronized?
Thanks again,
William Kane


If all your variables are local and none of them are references, or the references are to thread-safe Objects and/or are immutable, then you probably don't need the synchronized keyword in that instance.
[ November 06, 2005: Message edited by: Ken Blair ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!