Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a synchronized method

What happens when a method in super class is synchronized, but you override the method in a subclass and don't synchronize it ?

like image 935
user2434 Avatar asked Apr 16 '12 11:04

user2434


People also ask

Can synchronized method be overridden?

For example, an overridden synchronized method's contract can be violated when a subclass provides an implementation that is unsafe for concurrent use. Such overriding can easily result in errors that are difficult to diagnose.

Can overloaded methods be synchronized?

Yes. Overloaded methods can be synchronized.

What will happen if a synchronized method is called by two threads?

If you have two different instance methods marked synchronized and different threads are calling those methods concurrently on the same object, those threads will be contending for the same lock. Once one thread gets the lock all other threads are shut out of all synchronized instance methods on that object.

Can two synchronized methods in same class?

You cannot call two synchronized methods of a class on the same object. This is the use of synchronization. If you can call two synchronized methods on the same object then there is a chance of corruption of the state of the object. Synchronization helps you to avoid this....


1 Answers

If a method in super class is synchronized, but you override the method in a subclass and don't synchronize it, then the method is no longer synchronized if called on the subclass.

like image 167
xagyg Avatar answered Oct 11 '22 09:10

xagyg