Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which one of the following statements about the monitor a synchronized instance method locks is true?

  • A.A synchronized instance method locks the instance; a synchronized static method locks the Class object.
  • B.Both a synchronized instance method and a synchronized static method lock the instance.
  • C.Both a synchronized instance method and a synchronized static method lock the Class object.
  • D.A synchronized static method locks every instance of the class in turn.

Answer: A

Two different monitors are involved, which is why a synchronized static method and a synchronized instance method on the same class do not exclude each other: one holds the lock on the Class object and the other on an instance. Guarding static state with an instance lock therefore protects nothing.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.