Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could synchronized have been an annotation? [closed]

Tags:

java

Would it have been bad if, instead of synchronized keyword we had @Synchronized annotation? Would an annotation be more natural in this case (because you can override a synchronized method with a non-synchronized method - thus synchronized says nothing about the method itself but rather specifies something in addition to the method (that the method is guarded in a certain way), so it's not really a keyword?)

like image 578
Alexander Kulyakhtin Avatar asked Jan 15 '23 12:01

Alexander Kulyakhtin


2 Answers

synchronized is used on blocks of code and you can't add an annotation to a block of code.

You could have @synchronized on methods or even classes but Java didn't support annotations when Java was first introduced.

I think its important enough to deserves its own keyword. ;)

like image 175
Peter Lawrey Avatar answered Jan 28 '23 01:01

Peter Lawrey


Synchronized transforms practically directly into monitorenter/monitorexit on the byte code level. And you specify what to synchronized on, you can't do that with an annotation:

synchronized (myLock) {
}

So it makes perfect sense to me that it is a keyword.

like image 39
Durandal Avatar answered Jan 27 '23 23:01

Durandal