Synchronized threads inside of a SERVLET


G

gfrommer

Hello everyone,

I've written this very involved web application that allows users
to create and pass around reports of various forms. In the main
servlet, there exists two lists, activeReports and completedReports
where I use to store the two types of reports. In a couple of places
around the application, I need to move a report from the activeReports
ArrayList into the completedReports HashMap. To make sure no one is
iterating through either of the lists I used synchronized blocks like
this:

synchronized(completedReports) {
synchronized(activeReports) {

ArrayList al = (ArrayList)completedReports.get(rootTag);
if(al == null) { al = new ArrayList(); }
al.add(ReportObject);

completedReports.put(rootTag, al);
activeReports.remove(this);

}
}


If a user executes this bit of code while another user is iterating
through those lists, will there be problems?

Do I synchronize on those lists when I add/remove or when im iterating
through the lists. (Add/remove, because thats when its changing
structure... yes?)

Thanks everyone
 
Ad

Advertisements


Top