synchronized class aClassname { in which situation?
What we say '"synchronized" can be placed in either method or object,
I heard better use on method than object.
Why?
I've seen the code that was with method
synchronized deposit() {}
synchronized withdraw() { }
but not with object.
what would be in real-life implementation for putting
synchronized class aClassname {
}
I cannot think of it. what would be
> synchronized class aClassname {
You can't do that. The syntax of the synchronized statement is:
synchronized ( Expression ) Block
The type of Expression must be a reference type. As a convenience, a method may be declared synchronized; such a method behaves as if its body were contained in a synchronized statement. The locks acquired by synchronized statements are the same as the locks that are acquired implicitly by synchronized methods. If the method is an instance method, it locks the lock associated with the instance for which it was invoked. If the method is static, it locks the lock associated with the Class object that represents the class in which the method is defined.
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#255769
http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html#30206