1) "@Synchronized" is actually longer and with uppercase+lowercase
letters vs "syn" + autocomplete
2) the keyword shows up in the method definition and one knows which
monitor will be acquired
3) zero-length arrays as Objects with Monitors are indeed the best
choice for Serializable locks, but
do we really need to serialize a lock? No, one needs to mark the
locks as transient and create a
custom de-serialization method which will init the locks upon
deserialization.
Hence, I mark the @Synchronized annotation feature as not only
unhelpful (manual coding does the trick
just as fast) but also a serialization overhead
I agree with Maartin for all the points, but the serialization thing. I
didn't think of it before reading Victor's post, but I'd say that by
adding/removing a @Serializable to a method you're breaking backward
compatibility with existing serialized data, right? I'd say this is a
bug and the monitor should be really transient.
--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
java.net/blog/fabriziogiudici - www.tidalwave.it/people
Fabrizio...@tidalwave.it
--
You received this message because you are subscribed to the Google
Groups group for http://projectlombok.org/
To post to this group, send email to project...@googlegroups.com
To unsubscribe from this group, send email to
project-lombo...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/project-lombok?hl=en
"simply"? What if the class already contains a readObject method? The cost of non-final on the lock is also significant, in my opinion. Certainly more significant than "If you remove all @Synchronized, you break serialization of that class".
The reason the locks aren't public is because our philosophy is that they shouldn't be public. Locking is an implementation detail. A synchronized method hints that it might be thread safe in some fashion or another. �The docs will need to explain in which ways it is thread safe, because it's not a black and white issue. A method that does not have 'synchronized' in its signature can still be thread safe, so either way you have to look at the docs for the details. What good is synchronized, then?
As far as I know, a subclass can override a method and simply not add synchronized, so its significance is virtually zero. Hence the idea: That should never have been public information in the first place.
synchronized, the method modifier keyword, also uses 'this' (or Self.class for static methods) as lock object. So, a public lock then. public locks lead to deadlocks.
�--Reinier Zwitserloot
On Wed, Jun 29, 2011 at 5:44 PM, Victor Cordis <cordis...@gmail.com> wrote:
The way Java Serialization Overwriting is done is a bit ugly
(but there was no other way because of single inheritance (which is a
good thing) )
but can be an easy task, as specially in our lock case:
1) serialization:
� �simply mark the locks as transient and leaf it up to default
serialization
2) de-serialization:
� �simply define the private de-serialization method which would do
something like this:
� �private void readObject(java.io.ObjectInputStream s)
� � � � � �throws IOException, ClassNotFoundException {
� � � �// read the def serialization:
� � � �s.defaultReadObject();
� � � �// init the transient locks (which were not serialized)
� � � �this.mySafeLock = new Object();
� �}
� �The only down-side would be that the locks cannot be final anymore
� �( because the above method is not a constructor :-( � )
� �So the code could be more efficient ( time & mem) but you do not
� �have a "final" guarantee...
�
--
NB: Maaartin, if you use 3 @Synchronized in one class, you get just the one lock. It's a different lock from 'this' which was the point.
BTW: what's the big deal in loosing the "final" keyword of the lock
attribute if you
don't even get to see the attribute in your source code? You cannot
mess up what
you can't see.