Forums Register Login

thread

+Pie Number of slices to send: Send
Why local variables are thread safe while instance variables are not?
+Pie Number of slices to send: Send
Hi Ramm,

in short instance variables are shared among all threads accessing the same object. In contrast local variables are stored within a stack frame which is created for each method call. For this every thread calling a method gets its own copy of a local variable and can't interfere with other threads this way. Of course multi-threading is a lot more complicated but this is the basic difference between instance variables and local variables.

Marco
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 711 times.
Similar Threads
Threads
What is thread-local variable
Mock Question about thread-safe
Singleton or Prototype for DI bean injection?
question on instance variables in servlets
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:05:21.