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

Discussion

Which of these declarations will NOT compile?

  • A.void update() { synchronized (this) { } }
  • B.synchronized Thread() { }
  • C.synchronized void update() { }
  • D.static synchronized void reset() { }

Answer: B

A constructor cannot be declared synchronized, because no other thread can reach the object until construction finishes. Instance methods, static methods and blocks may all carry the keyword. A synchronized block inside a constructor is legal if a lock is genuinely needed.

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

Post your comment

Please log in or register to post a comment.