Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread safe instance variable

 
shiva shankar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a small doubt regd. the instance variable. Why is instance variable not thread safe ?

class abc {
static myclass abc;
myclass xyz;
}
Why is xyz not thread safe ?
Regds
Shiva
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to answer but then I realized that I do not like talking to web spiders.
Please read our naming policy and adjust your displayed name accordingly.
Thanks.
 
shiva shankar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is my name ok..now ? Answer please !!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say we started up two threads with the same unsafe object as an argument:

Then the two threads call some method in UnsafeObject:

We can think of two threads running "at the same time" but in truth the JVM runs a few instructions from one thread, then a few instructions from another and so on. One thread might work perfectly by itself. But when two or more are running, the JVM could switch control among threads between lines of code. I might call

and expect the second call to execute line 2, but if another thread changed unsafeVariable in between my two lines of code, the second call might execute lines 3 and 4.
Zat make sense?
 
shiva shankar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be too fundamental.
Is it an instance per thread or instance for many threads ?
if its the latter then what you say is perfect .
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello shiva.
The fields of an object can be shared between the threads that are able to access them. On the other way, local variables are only accesed by the thread that is executing the method where they were declared. That is, each thread calling the same method will have its own copy of the variables declared in it. But it that method accesses fields of an object, those fields are shared among all the threads executing that method (and possibly other threads executing other methods accessing the same variables)
That abc or xyz are not thread safe depends on their class, not on the instance or static characteristic.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member variables are per object instance. I showed the same instance of UnsafeClass as an argument to two threads, so both threads were looking at one object instance, one member variable instance. That's pretty much the definition of thread-safe or unsafe - how does it behave if two threads work with the same object instance.
This kind of thing happens in Servlet containers all the time. The container might have only one instance of a servlet object and pass it to any number of incoming request threads at the same time. It's also common for a main driver thread to spawn a bunch of worker threads and give each worker a reference back to the driver, perhaps to report their progress. Or maybe many threads want to update a window or log. So even if my example was a bit contrived (or a lot contrived) this does happen in the real world often enough to make you want to be careful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic