Win a copy of Murach's Python Programming this week in the Jython/Python forum!
    Bookmark Topic Watch Topic
  • New Topic

Synchronize a method  RSS feed

 
Yuma Shankar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have a situation where there is method that creates an ArrayList and populates the ArrayList with data from a database resultset. The application in question is a multithreaded application. Hence, when two users(threads) access this same method, the arraylist elements are getting out of wack-of course -since ArrayList is not synchronized. When I synchronize the method, it still does not help with the ArrayList even though the arraylist is local to the method. I do not understand that... Since by synchronizing I have a lock on the method, why does a object(the arraylist)local to the method not adhere to the lock?
Example below

public ArrayList getRecords(String className, String structure,
String condition)
{
ArrayList a = new ArrayList();

Object recordData,retval;

rs = getData(structure, condition, "query");
while (rs.next())
{ a.add(recordData);
}
}
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yuma,

Its strange that after making your method synchronized also it doesn't help. Can you print the Code of the changed Synchronized method to have a view of that.

Cheers,
Rahul
 
Yuma Shankar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
public synchronized ArrayList getRecords(String className, String structure,
String condition)
{
ArrayList a = new ArrayList();

Object recordData,retval;

rs = getData(structure, condition, "query");
while (rs.next())
{ a.add(recordData);
}
}
 
Yuma Shankar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am sorry, but I guess I should actually be posting this in the Java Forum. Will post it there...
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!