This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Synchronize ArrayList access in business objects?  RSS feed

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

I have a value object, SubordinateUserDTO (DTO == Data Transfer Object), that contains a collection (ArrayList) of another value object, OrgRoleDTO.

In addition to the usual getter/setters, I would like to add methods to add and remove OrgRoleDTO items from the collection. So the SubordinateUserDTO looks like this:



The value object is used to pass data back and forth between a business service object (Usermgmt) and a servlet that support a Flash client. Usermgmt has a method that returns an ArrayList of SubordinateUserDTO, like this:



My servlet creates a Usermgmt object and calls getSubordinateUsers() locally (not an instance variable) in a method that handles all requests. It seems to me this should work fine without synchronizing access to the two collections, but I'm not sure

1) Do I have it right in thinking that I don't need to synchronize access since my servlet creates and accesses the objects in a method and method calls get done on the stack?

2) Of course, the business object should support other callers besides the servlet. Should I make the SubordinateUserDTO.addOrgRole() and removeOrgRole() methods thread safe? If so, how? Also, what about making Usermgmt.getSubordinateUsers() thread safe? Should I do this, or at least warn folks in documentation?

Thanks for any input/feedback that you might offer,
Carl
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use Vector instead of the ArrayList class? Access to the elements inside a Vector is synchronized. Another tip, use List as the type as in
List orgRoleList;
that way you can change implementation any time.

Is the business service object an EJB? If not, you may have to worry about threads in the service layer.

Calls from within the servlet are thread safe because the container manages them.
 
Carl Trapani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chintan,

NICE!! Thanks much for your feedback! I changed the type to java.util.List and then changed the constructors to use Vector like this:

Is the business service object an EJB? If not, you may have to worry about threads in the service layer.

No, just a plain old java object. If I've implemented using Vector, than I should be ok?

At this point I'm wondering about the need for removeOrgRole(). I think I'll add a getOrgRole(int index) in it's place.

Thanks again!! Let me know if you see anything else.
Carl
Carl
[ September 02, 2005: Message edited by: Carl Trapani ]
 
What are you doing? You are supposed to be reading this tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!