What exactly happens when the @Synchronized annotation is applied to an extension function on Android/JVM? I expected that it would synchronize on the instance of the class, or maybe the class itself, but it appears to be doing neither. Does the annotation do anything in this situation?
P.S.: This is my first post, so I apologize in advance if I’m doing something wrong.
I don’t know the answer (and a quick web search doesn’t show it).⠀ But I wouldn’t expect it to synchronise on the receiver: note that you can define an extension function on a nullable receiver, so in that case at least, there may not be an instance to synchronise on.
My guess would be that it synchronises either on the class or object that the extension was defined within, or (if it’s a top-level function) on the object representing the file (usually the filename with
Kt appended, as you also see when specifying a top-level
main() function to run).⠀ But I don’t really know.
One way to check would be to write some code, compile it, then view/decompile the resulting bytecode.
Good call! I followed your advice, and it turns out that it’s synchronized on the object representing the file.
There’s a discussion about this controversial behavior here: https://youtrack.jetbrains.com/issue/KT-32469