Multiple choice

Which statement is true?

  1. A static method cannot be synchronized.

  2. If a class has synchronized code, multiple threads can still access the nonsynchronized code.

  3. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.

  4. When a thread sleeps, it releases its locks.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

2 is correct because multiple threads are allowed to enter nonsynchronized code, even within a class that has some synchronized methods. 1 is incorrect because static methods can be synchronized; they synchronize on the lock on the instance of class java.lang. The class that represents the class type. 3 is incorrect because only methods—not variables—can be marked synchronized. 4 is incorrect because a sleeping thread still maintains its locks.