Amazon Interview Question
Development Support EngineersSynchronized keyword in a method make it sure that the particular method is accessed by one and only one thread at a time. This is possible because in order to access a synchronized method, a thread need to acquire the lock associated with the method. In case of synchronized method, the lock is the same object on which the method is invoked. Now, as one lock can be acquired by only one thread at a time, the synchronized methods can be accessed by only one thread at a time.
Though I know the answer I got it wrong then. I said a lock will be acquired on the method which is wrong. Lock will only be acquired on Object in question but not the method.
- perllove January 19, 2008I succumbed to the trap.