Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrency between find and create methods in the data class

 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again all,

Excuse me if this has been addressed before, I am little crazy right now .

I was wondering how you guys handled the concurrency between the find & create methods.

I currently have the find method iteration over the "cache" object synchronized, which means I need to synchronize
the put on the cache in the create method. This bothers me because technically on a search, only one thread would be able to perform
that operation at a time, when no "creates" are ongoing. (Its like synchronizing the find method completely and only allowing 1 thred to access it at a time) I was wondering if that concerned anybody and how you handled it
 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't bother at all: every method is marked synchronized, so yes only 1 thread at a time can perform a method. It's not that performant, but it's a very easy approach. If you want your application to be more performant, you can work with synchronized blocks instead.
 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Roel,

Thanks for the response... Actually, I am working with synchronized blocks just for that reason, because synchronizing the methods
only provides so much protection. You still have to provide synchronized blocks right?

Thread 1 ---> inside synchronized method 1 iterating through object a

Thread 2 --> inside synchornized method 2 modifying object a

This scenario has the possibility of concurrentmodification exception.
 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are semantically equivalent:

If you synchronize the complete method I don't see why you would need to provide synchronization blocks. So that's not right at all! If thread1 is in synchronized methodA of objectX, then thread2 (which wants to execute synchronized methodB of objectX) simply has to wait until thread1 has finished executing methodA.
 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I dont know why I didnt realize that.

I have been synchronizing on individual objects,

lock method -- synchronizes on the room object
unlock -- synchronizes on the room
find -- synchronizes on the data cache
create -- synchronizes on the data cache

Do you think it would be better to try and synchronize the lock method on (this)? as well as all teh other methods.... My locking mechanism
is performing quite well... no deadlocks, but I am concerned that there might be something i missed.. that chance of throwing that exception or
deadlocking.


 
Roel De Nijs
Sheriff
Posts: 10400
133
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems a weird approach. You would expect that your lock/unlock methods synchronize (at leadt) on the map with locked records, because that's the structure you are reading from and writing to, so you would not want that to be changed from another thread while you are manipulating this structure.
I would advice to synchronize all methods on the same object (this) instead of every method synchronizing on another object, just for the sake of simplicity. This might be a bit less performant, but according to the instructions simplicity is preferred above performance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:I would advice to synchronize all methods on the same object (this) instead of every method synchronizing on another object, just for the sake of simplicity. This might be a bit less performant, but according to the instructions simplicity is preferred above performance.

That's what I did, with a comment in my choices document that I'd revisit it if load testing showed there was a performance problem.
 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was just a little hard to follow...

I was synchronizing sometimes on multiple objects, as you said, the lockedRecords map, and the room object, the datacache, sometimes multiple in an method... but overall, I had no deadlocks, and the performance was great.... but it does leave an opening for holes... and when someone comes in to maintain
that, they might move something and not necessarily understand why I did what I did.

I switched to synchronizing on this and although it looks good from a deadlock standpoint, it is much slower... no matter..

But your right, and its somethign I should have just done from the beginning, which is make it simple.. Thank you two for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic