Static method called from different methods at the same time?

Hi,

We have a utility class on the serverside that has a lot of static methods.

The server is called from a lot of different clients. If two clients call the

server at the same time. What happens if both the clients call one of

these static methods? Does the variables in the method get assigned

new values when it gets called the second time and the first call isn磘

completed yet?

Can you have static methods when they might get called simultaneously

from more then one metod?

Thanks!:-)

[556 byte] By [Lisa_Ra] at [2007-11-26 14:55:03]
# 1

The local variables in the static method are all kept on the thread running the method.(The same applies to local variables in a non-static method.) That means two different threads can be running code that calls the method at the same time, and the local variables will work independantly (and won't clobber each other).

However, if the static method changes any static fields in the class, then if two methods call it at the same time bad things can happen.

For a static utility class, no methods should change any static fields on the class. The class shouldn't have any fields, static or otherwise; otherwise arguably by definition it could not be a "utility" class.

paulcwa at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 2
> The class shouldn't have any fields, static or otherwise;final static String s="except maybe final ones";
BIJ001a at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

you should have used a singleton class instead of an utility one.

The singleton pattern ensures that a unique object of a class is used and so the common resource(s) is(are) accessed by at most one client at the same time.

here is a singleton sample:

public class myClass{

public static final myClass instance = new myClass();

private myClass(){

}

public static final getInstance(){

return instance;

}

}

google "java singleton pattern" for further information

java_2006a at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 4
> the common resource(s) is(are)> accessed by at most one client at the same time.A singleton doesn't ensure that. If by "client" you mean "thread": it still doesn't do it. This is what "synchronized" is for.
CeciNEstPasUnProgrammeura at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 5

> > The class shouldn't have any fields, static or

> otherwise;

>

> > final static String s="except maybe final ones";

>

Yeah. I guess my point is, if it's a "utility class", then it shouldn't have any state (data which can change over time). If it does have state, then it's not a utility class and you should give it a rethink, either dropping the "utility class" fiction or moving the state somewhere else.

paulcwa at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks! :-)
Lisa_Ra at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...
# 7
> you should have used a singleton class instead of an> utility one.Why?
mlka at 2007-7-8 8:43:32 > top of Java-index,Java Essentials,Java Programming...