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.
Java synchronisation only works if the object is locked by all threads. If the java threads are locks for different instances of class objects, then the synchronisation will not work. All java threads should try to lock the same object, and if the lock is not available, the thread will wait until the lock is released.
Lock framework works like synchronized blocks except locks can be more sophisticated than Java's synchronized blocks. Locks allow more flexible structuring of synchronized code.
Yes, they can run simultaneously both threads.
There is nothing that says the main function can only be used as the entry point to a program. Being static, there is no reason other classes couldn't call SynchronisizedMain. main() . Being synchronized prevents multiple instances from being executed concurrently which might be desirable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With