views:

135

answers:

3

My question is about threads being queued. For my example I have one Spring context. I have a method named CalculateTax in a stateless class. A request comes in, a thread is created (tA) and it eventually enters the CalculateTax method. Within the same "time frame" another request comes in and another thread is created (tB). Now, here is what I want to understand. AFAIK tB cannot execute CalculateTax until tA has exited the method. Is this true?

+2  A: 

No it is not true if they are parallel thread, each thread is in its own stack of execution so it should be able to execute while tA is executing.

This is what Threads are for.

Lex
thanks - just what I needed
And unless you didn't make the method synchronized.
Adeel Ansari
Totally right, unless you didn't make them sync
Lex
Why would I need to mark the methods as synchronized?
No, that's not that you need to... but if you do each Thread WILL wait for the others to end their operation before executing itself.
Lex
+3  A: 

As long as CalculateTax only uses local variables (i.e. declared in the method), you will not have any thread sync issues and multiple threads can call the method without a problem.

However if for some reason CalculateTax uses variables defined at the class level, and you are using the Singleton pattern (you tagged your question with "singleton", so I guess you are), you may have thread sync issues.

JonoW
He said the class is stateless. So...
Adeel Ansari
Ah didn't see that, thanks
JonoW
A: 

Generally speaking the answer is undefined. If your 'request' comes from remote client the answer depends on implementation details of the mechanism used for service exposing.

However, I'm not aware about remote communication frameworks that really make the proxy serializing the requests, i.e. that is assumed to be addressed by target service developer (e.g. its your task to provide thread-safety for the service implementation OR serialize all requests using explicit synchronization etc).

denis.zhdanov