Can somebody clarify a long standing question of mine related to synchronized keyword in Java. Let me explain the way I understand it(correct me if am mistaken).....
The synchronized keyword is used to obtain a lock on an object mentioned in the synchronized line, i.e. 'synchronized (someObj)' means lock on 'someObj'. This 'someObj' can also be 'this'. Also in Java I can synchronize a method by saying (say) 'public synchronized void someMethod().....'
In this case is the lock obtained implicitly on 'this' object? If so, then it means synchronizing a method and writing 'synchronized(this)', is equivalent, right?
If the above statements are true then this would mean that if I have a object which has synchronized methods, then only one method at a time can be invoked on the object. Eg. RMI server object (class that extends UnicastRemoteObject).
And finally, if I assume that the above statements are true and I consider a a weird case where I have a class that has to have synchronized methods and there are no fields in that class (static or non static). Also my methods are not dependent on each other. Then the best way would be to have an object for each method in the class where the purpose of the object would be just to act as the one on which a lock for that method is obtained.
> if I have a object which has synchronized methods, then > only one method at a time can be invoked on the object.
Only one synchronized method can be invoked at a time. Non-synchronized methods will not be blocked.
> there are no > fields in that class (static or non static). Also my > methods are not dependent on each other.
My first thought is that the methods should be in different classes. Each class should have one clear responsibility, and it sounds like the one you're describing has many responsibilities.