• Post Reply Bookmark Topic Watch Topic
  • New Topic

Local variables in Static methods - will multiple concurrent calls to method will violate result?  RSS feed

 
Nilakshi Weerakoon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please consider the below code.

code1:
-------


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:

code2:
--------


If there're multiple calls to code2 concurrently, then will 'mainDeal' be thread safe?


Thanks in advance!

[devaka: added code tags]
 
Devaka Cooray
Marshal
Posts: 5516
687
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Nilakshi, welcome to JavaRanch

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.

Devaka
 
Nilakshi Weerakoon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Devaka
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!