Difficult Java interview questions – Threads and Synchronization

I was discussing with a colleague about a few questions which she was asked about Synchronized keyword in java. Following are the excerpts from that discussion:

a. What is the primary difference between Synchronized method and Synchronized block in Java?
Ans: The answer is obvious.The former Synchronizes on a method call and the later on a block of java code. The basic difference though is, in Synchronized method you can lock the object on which you are calling the method. However, in case of Synchronized block, you can pass any object(member variable) which you want to synchronize. e.g. (this)/(list), etc


public class MyClass {
   List list=new ArrayList();
   public synchronized void foo() {
// do something
   }
   // similar
   public void foo() {
      synchronized(this)|(list) {
      // do something
      }
   }
}

b. How does the static keyword affect Synchronized method and Synchronized block in Java?
Ans. With the static keyword before a Synchronized method, the lock is acquired on the Class rather than the object. Hence, if there is another synchronized static method in the same class, it cant be called at the same time.


public class MyClass {
   // locks MyClass.class
   public static synchronized void foo() {
// do something
   }

   // similar
   public static void foo() {
      synchronized(MyClass.class) {
// do something
      }
   }
}

I think even the static methods cant be called when we have acquired a lock on a Class(Need to verify though).

On a side note, if we want to synchronize two methods of the class, such that only one thread can execute a method at a time, we can do the following:


class SomeClass {
    private final Object lockA = new Object();
    private final Object lockB = new Object();

    public void methodA() {
        synchronized (lockA) {
            // 
        }
    }

    public void methodB() {
        synchronized (lockB) {
            // 
        }
    }
}

c. What happens in the below code?


class A {
    public synchronized void myOneMethod() {
        // ...
    }
}

class B extends A {
    public synchronized void myOtherMethod() {
        // ...
    }
}

// ...

B myObject;

// ...

myObject.myOneMethod(); // acquires lock
myObject.myOtherMethod(); // On which object the lock is obtained?

Ans: A lock is a lock on the myObject(instance of B) doesnt matter which method is called.
d. How do you share state between two threads in Java?
Ans: Define a shared variable in the calling class and define the type as ArrayBlockingQueue. Then, if you would like, synchronize the access to the variable in a synchronized block.

e. Can we synchronize constructors in Java?
Ans: No. Results in a compile time exception. A thread which is creating the object is the only thread which is creating the instance until it completes creating the thread

f. Can a thread acquire a monitor/lock on itself ?
Ans: Yes, it is possible. Synchronized methods are re-entrant. i.e. a synchronized method -methodA() can call another synchronized method -methodB() of the same object.

g. If there are two synchronized methods m1,m2 in a Class C, can two different threads t1,t2 call different methods(m1,m2) respectively on the same object of Class C at the same time?
Ans: No. Only one thread can hold the lock on an object of a class. However, the other non-synchrnoized methods can be called on the same object.

Courtesy : StackOverFlow , GeekExplains

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s