Local variables in Static methods - will multiple concurrent calls to method will violate result?
posted 6 years ago
Please consider the below code.
the above static method is called inside an EJB, and the deal (of type Deal) created in the bean is input to the method. (not returned, only modified)
Consider, If I have multiple calls to the above method at the same time. Will 'deal' be thread safe?
Consider the 2nd code example below:
If there're multiple calls to code2 concurrently, then will 'mainDeal' be thread safe?
Please UseCodeTags when you post a code at JavaRanch. I've added code tags for you.
Your first method may concurrently modify the Deal object in a thread-unsafe manner, if you passed the *same* Deal object in concurrent method calls. As a side note, you may consider using something like stateless session beans instead of using static methods.
You second method is perfectly thread-safe, because it will create a NEW Deal object at every time when the method gets executed.