• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this thread safe?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a static variable of type A
A has a non-static method called B.

B does not reference any static variables (naturally, because it won't compile if it did) but calls other non static methods which also do not reference any static variables.

Is this thread safe?
Meaning if two threads call method B, they will not corrupt each other's data...
From what I understand, local variables are placed in a heap per calling thread, so each thread would have its own copy...
Am I right?
[ June 25, 2004: Message edited by: Oliver Chua ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
say ur class A is

class A{
int i = 0; // non-static instance var
public void B(){
i += 10;
}
}

Now say there is a instance(static/non-static, doesnt matter)of A and two threads get hold of it. If both of them call B() at the same time on this instance, you may see a clash.

So, unless your method uses strictly method local variables or input arguments which are final references to immutable objects, it is not thread safe. Ofcourse, all this is assuming you are not using the 'synchronized' modifier.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You'll need to clear up exactly what you're asking:

"I have a static variable of type A
A has a non-static method called B."

A variable A, static or otherwise, cannot have a method B.

"B does not reference any static variables (naturally, because it won't compile if it did) but calls other non static methods which also do not reference any static variables"

A non-static method CAN access any variables or methods, static or otherwise, as long as they are visible within that method.

"Is this thread safe?
Meaning if two threads call method B, they will not corrupt each other's data..."

If two threads call method B on the same object instance, there is the potential for threading problems. It depends on exactly what this method does, but it is always safer to mark the method 'synchronized' if you are not sure.

"From what I understand, local variables are placed in a heap per calling thread, so each thread would have its own copy...
Am I right?"

Nearly right - method local variables are placed on the STACK per calling thread and are not vulnerable to threading issues. If you have a method that only uses local variables, or calls other methods that themselves only use local variables, then it is safe to have the method called by multiple threads.

It's difficult to determine exactly what you're tryng to do though - would you be able to provide some sample code?


 
reply
    Bookmark Topic Watch Topic
  • New Topic