I am building a stock system which will be used over an office LAN with multiple users. I have a query about using the synchronized keyword to update the stock correctly. What I wish to do is allow multiple users to update the stock but of course only allow one user to update at one time. I have created a method as follows for the update of stock:
public static synchronized boolean UpdateXYZStock(Stock so){
//update code
}
Is this the correct way to do this?
Thanks
S.
I would lock on an instance of the object, not the class. i.e. don’t lock a static method since you’re locking the class. Further to that you may want to lock on an underlying object e.g.
so you can control the granularity of the locking more finely (I’m assuming this method is on a component within a server and thus serving multiple clients)