Tuesday, August 17, 2010

Synchronized: Ska.java

Courtesy of Sun Web Learning Center



 5. public class Ska {
 6.   synchronized long fellow;
 7.   synchronized void doLockedStuff() {
 8.     Ska s = new Ska();
 9.     synchronized() { }
10.     synchronized(s) { }
11.     synchronized(this) { }
12.   }
13. }

What is the result?


A     Compilation succeeds.
B     Compilation fails due to errors on lines 6 and 9.
C     Compilation fails due only to an error on line 6.
D     Compilation fails due only to an error on line 9.
E     Compilation fails due only to an error on line 11.
F     Compilation fails due to errors on lines 6, 9, and 11.


B is correct. You cannot synchronize an instance variable (line 6), or a code block without an object (line 9).

No comments:

Post a Comment