• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with multi-threading and synchronization

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an issue with multiple-threading and the create method. I have synchronized access to my create method. I also have a cache to hold a listing of valid record numbers for valid record entries in my database. I have an issue though, if say I run a 100 users to access this valid record numbers cache. In some cases, it works, say is able to get 100 valid record numbers from the cache, increment them then, in order to create new record numbers but in other cases it may just create 99 or 98 valid record numbers. In the latter case, in fact it may repeat some record numbers already created, for example, say 29 entries previously existed in database and i try to create a 100, then new record number 57 could be repeated twice, for example and therefore over written in the database. I thought though if I made the method synchronized, there shouldn't be a problem with threads interacting with each other except for class instance variables and class static variables. Here's roughy the code I am having issues with:

The validRecordsCache holds the integer record number values to database offsets. Surely if my methods are synchronized, am issue shouldn't occur in this case, if I run say 100 threads? For example, sometimes I could get 2 threads with 'newRecordNr' of 57 for some reason, even though I have synchronized the method? My understanding of synchronized in this case is that the method (object) muse be completed by 1 thread before another can begin. Thanks.
 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
Have most of this problem solved. Thanks.
 
Roel De Nijs
Sheriff
Posts: 10665
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark O' Sullivan wrote:Sorry, Have most of this problem solved. Thanks.



It would also be nice to give some (small) description how you solved it or indicate the cause of the problem. So other ranchers with the same (or similar) issue will have a clue about a possible cause and/or a way to solve the issue.

Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic