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

Discussion

Which of these is a legal way to synchronize on a private lock object?

  • A.synchronized lock { count++; }
  • B.synchronized (lock) count++;
  • C.lock.synchronized { count++; }
  • D.synchronized (lock) { count++; }

Answer: D

The statement takes a parenthesised expression followed by a BLOCK in braces, so both the parentheses and the braces are required. Unlike if or for, a synchronized statement cannot take a single statement without braces, and there is no method form.

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

Post your comment

Please log in or register to post a comment.