Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What happens when concurrent requests call the same static Java method running on an application server?

Say that a method run takes 2 seconds and receives 100 calls in a given second. The method itself wouldn't read/write outer state.

Could it be better to let it be an instance method instead?

share|improve this question
up vote 5 down vote accepted

If the method does not access/change any external state, it is in fact a function. There's nothing wrong with that (some programming languages are based entirely on functions), and it will not cause any performance problems (at least not due to being static).

share|improve this answer
That's what I was precisely trying to achieve - a functional-like architecture. I'm already aware that there's nothing wrong with functions per se but I was just wondering how are they implemented in a typical Servlet - which mostly abstracts one from the thready nature of the web. – vemv Dec 19 '11 at 18:26

Does your method change any state (e.g. Is it mutable)? If not then you're probably quite safe to make it a static method. However, it's always best to test these things out.

When you get to the testing part, try simulating your concurrent threads using JMeter.

share|improve this answer
I think that if the OP is asking about thread-safety, he might have issues setting up JMeter. He may be better of just trying to understand the responses here... – jjmontes Dec 19 '11 at 16:59
Thanks for the answer. I'll give a go to JMeter anyway, looks like it's gonna be my dev server's new best mate. – vemv Dec 19 '11 at 18:30

static as far I can explain means that whatever is declared with static will exist in a single place in memory as a single instance for everyone, kind of a shared variable or function.

so generally is not a good idea to declare a static variable that is not constant unless you need something like a global counter.

However you may have a static function that does not change any values and only accepts values a returns other values, in that case I see no problem there.

However how Martijn explained is always better to test your code understress. I for example don't know what to expect of static functions that call objects

share|improve this answer

Concurrent requests should be handled by your web server. There should be no difference between accessing a static function versus an instanced function so long as no state is modified externally. Each request will simply call and execute the function regardless of whether another existing call is running. I believe the only concern should be whether your server can handle the load of calling that function 100 times inside of a second.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.