Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Methods

 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the method

public static float compute(float a, float b) {
// Some calculations here that take a long time.
// We are using a lot of local variables here.

return someValue;
}

- If two different threads are accessing compute method and enter the method at the same time, is there a chance of thread 1 not getting the perfect results for the values it has passed or vice versa. (I believe it should not, wish to confirm.)

- How is the memory allocated in Java Heap for a static method. We know that every method runs has its own stack when we do not have static. But what when the method is static ?

Cheers,

Nitin Dubey
 
Rick O'Shay
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every method has its own stack frame for parameters and local variables. Methods are therefore thread safe. Notice I have not qualified that for static or non-static because it's always true, no matter what.

A static method does not get an implicit object reference (this refrence) but it has no direct relevance to thread safety or heap object allocation.
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to be honest i don't think Rick is right ...

static methods too are also have to be syncronized if you(programmer) think that other thread will also access that static method for its calculation and that has some storng effect on whole programe if we don't syncornized...
see here we syncronized just because we don't want two thread fight each other for method to complete...
if we don't put syncronized on method that has some strong effect ..
two thread run the method in turn without completing to its end...
till one thread run that method fully...


any other explanations... ??
[ June 20, 2005: Message edited by: amit taneja ]
 
Nitin Dubey
Ranch Hand
Posts: 126
Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies guys, I am still not sure about this. Ya we do synchronize the things when we want only 1 thread to execute a portion of the code. But what about a situation when you have a static method with local variables ? How is the memory allocated ?


Nitin
 
Joji Doi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If the computation involves with member variable(s), then the synchronization needs to be taken into account. If the computation involves only with those premitive types passed from caller and any number of local variables, there is no need of synchronization.

Unless local variable references to an instance which contains a static member variable and this value is modified in the process. Then this critical section might be synchronized.


Cheers! _Joji
 
shetal bansal
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain the above text with an example!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic