static funtions

H

hula

Hello

i have some simple question reguarding static functions:
If i have some class that implements a static function that is called
from many places inside the app would every call have to wait for
termination of a previous call to that static function?
The function is not defined as synchronized.

If not, how is this realized in java? I mean if i understood it right
static functions are functions of the class not of any object so this
function should not be present more than once in memory right? Would i
overwrite the functionstate if i'd call the function eg. from more
threads?

_thanks for your info in advance

PS: the function does some DB calls in a tomcat app where some thousend
people call the function within short time and i'd like to avoid the
waiting, so i thought of changing the function to be nonstatic but
first i'd like to understand how static functions work :)
 
P

Patricia Shanahan

hula said:
Hello

i have some simple question reguarding static functions:
If i have some class that implements a static function that is called
from many places inside the app would every call have to wait for
termination of a previous call to that static function?
The function is not defined as synchronized.

If not, how is this realized in java? I mean if i understood it right
static functions are functions of the class not of any object so this
function should not be present more than once in memory right? Would i
overwrite the functionstate if i'd call the function eg. from more
threads?

Java has a stack for each thread. When a function is called, it is given
space for its local variables on the stack of the calling thread. The
code can be shared by any number of threads, because it does not change.

If you don't need synchronization to protect e.g. static fields that are
shared between threads, you should be fine.
_thanks for your info in advance

PS: the function does some DB calls in a tomcat app where some thousend
people call the function within short time and i'd like to avoid the
waiting, so i thought of changing the function to be nonstatic but
first i'd like to understand how static functions work :)

Depending on the queries and the database manager, the database may be
doing some locking of its own. Also, do you have the I/O bandwidth to
support the target query rate?

Patricia
 
B

bjeremy

hula said:
Hello

i have some simple question reguarding static functions:
If i have some class that implements a static function that is called
from many places inside the app would every call have to wait for
termination of a previous call to that static function?
The function is not defined as synchronized.

If not, how is this realized in java? I mean if i understood it right
static functions are functions of the class not of any object so this
function should not be present more than once in memory right? Would i
overwrite the functionstate if i'd call the function eg. from more
threads?

_thanks for your info in advance

PS: the function does some DB calls in a tomcat app where some thousend
people call the function within short time and i'd like to avoid the
waiting, so i thought of changing the function to be nonstatic but
first i'd like to understand how static functions work :)

As long as the static function is reentrant it should ok in a
multi-threaded environment... By reentrant I mean that it does not hold
static data over a period of time, return a pointer to static data or
call non-rentrant functions.. if not you need to synchronize access.
 
K

kingpin+nntp

hula said:
i have some simple question reguarding static functions:
If i have some class that implements a static function that is called
from many places inside the app would every call have to wait for
termination of a previous call to that static function?
The function is not defined as synchronized.
[sNip]

There's a very easy way to find out -- just write a static function
(method?) that includes the following line:

java.lang.Thread.sleep(32000); // Sleep for 32 seconds

Then, call that function from two separate threads at approximately the
same time and see if one of them requires up to 64 seconds to complete
the call. If they both complete within roughly 32 seconds, then
synchronization isn't a problem in the manner you're concerned about.

I hope that helps.
 
S

Steve

What you are describing is heading down the path to a "singleton
pattern", and since it has a fancy name, it must be a good thing, right? :)

I suppose it's worth mentioning that the multiple users aren't going to
wait for the "termination of the previous call". The way threading
works isn't granular by function call, or even by statement. I suspect
that database considerations like transaction locking will have a bigger
impact on performance.

It seems to me that for a method that calls transaction-oriented code in
a DB, it might actually be worth having it static (or at least done
through a singleton instance) and synchronized, in order to avoid the
delays waiting for data access.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top