devsda devsda - 21 days ago 5
Java Question

Why synchronized method is not included in interface

When I use

synchronized
on a method in an interface, the compiler emits an error. Can you tell me why this happens?

What is the reason (logically) that
synchronized
cannot be applied to a method on an
interface
?

I tried to make an Interface over Threadpool in this link.
Help me to make Interface in my above code.

Answer

Because synchronized is an implementation detail. One implementation of the method might need to make the method synchronized, whereas another one might not need it. The caller doesn't care whether the method is synchronized or not. It's not part of the contract, which tells what the method does. Which synchronization technique, if any, is used to fulfill the contract is irrelevant.