Monday, August 1, 2011

My favorite Java Concurrency interview questions with answers?

1. What is deadlock in Java? How to detect it and prevent it?

Ans : Multiple threads waiting forever due to cyclic locking dependency.

Thread T1  holds lock L1 and tries to acquire lock L2 BUT at the same time

Thread T2 holds L2 and tries to acquire L1. Both threads will wait forever.

Types of deadlocks
  •   Lock ordering deadlocks : 
  •   Nested Lock acquisitions
  •   Resource deadlock

Avoiding Deadlocks
  1. lock ordering : http://jaroslav-sedlacek.blogspot.com/2011/03/deadlock-despite-consistent-lock.html
  2. timed tryLock feature of explicit Lock classes.
  3. use thread dump for analysis.

Synchronized methods (in Java):


Another favorite question of interviewers is, "What is a synchronized method in
Java?" Each object in Java has its own mutex. Whenever a synchronized method is
called, the mutex is locked. When the method is finished, the mutex is unlocked.
This ensures that only one synchronized method is called at a time on a given object.


Deadlock:


Deadlock is a problem that sometimes arises in parallel programming. It is typified
by the following, which is supposedly a law that came before the Kansas legislature:
"When two trains approach each other at a crossing, both shall come to a full stop
and neither shall start up again until the other has gone."
Strange as this sounds, a similar situation can occur when using mutexes. Say we
have two threads running the following code:




Thread 1:
acquire(lock1);
acquire(lock2);
[do stuff]
release(lock1);
release(lock2);
Thread 2:
acquire(lock2);
acquire(lock1);
[do stuff]
release(lock2);


release(lock1);


Suppose that thread 1 is executed to just after the first statement. Then, the
processor switches to thread 2 and executes both statements. Then, the processor
switches back to thread 1 and executes the second statement. In this situation,
thread 1 will be waiting for thread 2 to release lock1, and thread 2 will be waiting
for thread 1 to release lock2. Both threads will be stuck indefinitely. This is called
deadlock.




How can we ensure that deadlock does not occur?


Answer: There are many possible answers to this problem, but the answer the
interviewer will be looking for is this: we can prevent deadlock if we assign an order
to our locks and require that locks always be acquired in order. For example, if a
thread needs to acquire locks 1, 5, and 2, it must acquire lock 1, followed by lock 2,
followed by lock 5. That way we prevent one thread trying to acquire lock 1 then
lock 2, and another thread trying to acquire lock 2 then lock 1, which could cause
deadlock. (Note that this approach is not used very often in practice.)






What is polymorphism?


Interviewers love to ask people this question point‐blank, and there are several
possible answers. For a full discussion of all the types of polymorphism, we
recommend looking at its Wikipedia page. However, we believe that a good answer
to this question is that polymorphism is the ability of one method to have different
behavior depending on the type of object it is being called on or the type of object
being passed as a parameter. For example, if we defined our own "MyInteger" class
and wanted to define an "add" method for it (to add that integer with another
number), we would want the following code to work:
MyInteger int1 = new MyInteger(5);
MyInteger int2 = new MyInteger(7);
MyFloat float1 = new MyFloat(3.14);
MyDouble doub1 = new MyDouble(2.71);
print(int1.add(int2));
print(int1.add(float1));
print(int1.add(doub1));
In this example, calling "add" will result in different behavior depending on the type
of the input.