This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Is this thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is this thread safe?" Watch "Is this thread safe?" New topic
Author

Is this thread safe?

Oliver Chua
Greenhorn

Joined: Feb 27, 2004
Posts: 29
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 ]
siva raju
Ranch Hand

Joined: Oct 10, 2003
Posts: 37
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.


Siva<br />-----<br />SCWCD 1.4, SCJP 1.4, SCBCD(Preparing), CCNA
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108

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?


 
jQuery in Action, 2nd edition
 
subject: Is this thread safe?
 
Similar Threads
Static Methods with local variables.. Thread Safe?
A question regarding to thread in Chapter 9 of SCJP study guide
SingleThreadModel question
Static Methods
Are local objects thread-safe?