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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top