Before which of the following can the "synchronized" keyword be placed, without causing a compilation error?
Options :
A. Class Methods
B. Instance Methods.
C. Any block of code with in a method.
D. Variables.
E. A Class
Answers : A, B and C.
The synchronized key word can used used to synchronize a class method as well as instance method. Synchronized key word can also be used to synchronize a block instead of whole method.
public class Counter {private int count = 0;public void increment() {synchronized (this) {count++;}}public int getCount() {synchronized (this) {return count;}}public synchronized static void print(){System.out.println(count);}}
No comments:
Post a Comment