how to serialize function depending on what instance of object calls it, if same instance call in a thread then do serialize else not

Posted by LondonDreams on Programmers See other posts from Programmers or by LondonDreams
Published on 2012-04-06T07:02:48Z Indexed on 2012/04/06 11:41 UTC
Read the original article Hit count: 188

I have a function which fetches and updates some record from db and I am trying to make sure each if the function is called by same instance of object(same Or different thread) then function should behave synchronized else its a call from different object instance function need not to be synchronized.

I have tried it use a lock per client. That is, instead of synchronizing the method directly using explicit locking through lock objects using Map.

function is like :-

getAndUpdateMyHitCount(myObjId){ 
//go to db and get unique record by myObjId 
//fetch value , increment , save update 
} 

And this function may get call is same thread by different Or same object instance But as fetching and matching from Map is slow , Is there other optimized way to do this ?

Found similar at this Question but dont feel that is optimized

© Programmers or respective owner

Related posts about java

Related posts about code-quality