wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to serialize function depending what instance of object calls it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to serialize function depending what instance of object calls it" Watch "how to serialize function depending what instance of object calls it" New topic
Author

how to serialize function depending what instance of object calls it

sankalp suryawanshi
Greenhorn

Joined: Feb 03, 2008
Posts: 4
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 http://stackoverflow.com/questions/7427851/change-synchronizing-of-function-at-runtime but dont feel that is optimized
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi sankalp suryawanshi,

Welcome to CodeRanch!

In your question, where does serialization come in picture? All I see is you are fetching from and updating the value to DB.

If you want that update operation to be synchronized, then you must ensure that no two concurrent requests should go to DB.

Hint : The method (or the relevant code) must be synchronized across the objects (i.e. no matter which object is calling it). If you synchronize it on 'this', then it won't work.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
sankalp suryawanshi
Greenhorn

Joined: Feb 03, 2008
Posts: 4
Anayonkar Shivalkar wrote:Hi sankalp suryawanshi,

Welcome to CodeRanch!

In your question, where does serialization come in picture? All I see is you are fetching from and updating the value to DB.

If you want that update operation to be synchronized, then you must ensure that no two concurrent requests should go to DB.

Hint : The method (or the relevant code) must be synchronized across the objects (i.e. no matter which object is calling it). If you synchronize it on 'this', then it won't work.


Your are correct there is no synchronized in the function but I need to implement it when same instance of object in the thread calls the function again. While in same thread another instance of object calls the function I dont need synchronization as different instance has it diff identifier for db.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

So, if I understand correctly,

1) If same instance calls the method via different threads, then the method should act like synchronized.
2) If different instance calls the method via different threads, then the method need not be acting like synchronized.

Well, it is quite easy if this is the case. If you still find it difficult, then just take a look at synchronization chapter from any descent Java book.

I hope this helps.
sankalp suryawanshi
Greenhorn

Joined: Feb 03, 2008
Posts: 4
Anayonkar Shivalkar wrote:So, if I understand correctly,

1) If same instance calls the method via different threads, then the method should act like synchronized.
2) If different instance calls the method via different threads, then the method need not be acting like synchronized.

Well, it is quite easy if this is the case. If you still find it difficult, then just take a look at synchronization chapter from any descent Java book.

I hope this helps.


ahh it is so clear now . Thanks a ton.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to serialize function depending what instance of object calls it