This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a session bean, which calls some methods of two helper classes( utility class). Since helper classes only provide some utilities, I'd like to define them as static classes.
In the context of container, bean is of no concern about multiple client access. But what if multiple bean instances access to single helper class? Is it necessary to add "synchronized" to some methods of two helper classes? Second case, if i didn't define helper class as static, then is it true that there are two hepler class for each bean instance? Is "synchronized" stilled needed? Why? In this case, can helper classes get the same concurrent control service from container as bean itself? For the performance view, which case is better? Thanks for your reply.
Joined: Sep 29, 2002
Hi Gurus, I need your help. I have been puzzled by this for a long time. Thanks in advance.
Cindy, If your session bean methods create new instances of the helper class then there is no need for synchronization. Synchronization is only necessary for shared data -- and in this case the instances would not be shared. This is really not J2EE specific -- I'd suggest that you read up on standard threading principles in Java -- maybe Doug Lea's "Concurrent Programming in Java" might help. Kyle
Thanks for reply. Since helper classes are just utility classes, I prefer to define them as static classes. Then it means all bean instances share the same helper objects. In this case, I should add "synchronized" to setter method, right? From the perspective of performance, which is better--case 1, static helper classes shared by all bean instances with synchronization control; or case 2, each bean instance with its own helper class? Thanks in advance.
Do yourself a favor. Don't design for performance. Design for clarity. If performance becomes an issue than use a profiler to find out where the bottleneck is and fix it then and there. [ October 02, 2002: Message edited by: Chris Mathews ]