Search This Blog

Friday, January 28, 2011

Question 13

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);
  }
}
Question 13SocialTwist Tell-a-Friend

No comments:

Post a Comment