Monday 30 April 2012

JAVA interview questions 30

Q291. What is object lock? Explain when it is required? 
Ans. Every object in java has a unique lock whenever we are using synchronization concept then only lock concept will coming to the picture. 
If a Thread wants to execute a synchronized method first it has to get the lock of the object. Once a Thread got the lock then it is allow to execute any synchronized method on that object. After completing synchronized method execution Thread releases the lock automatically.While a 
Thread executing synchronized method on the given object the remaining Threads are not allow to execute any synchronized method on that object simultaneously. But remaining Threads are allow to execute any non-synchronized method simultaneously. (Lock concept is implemented based on object but not based on method.) 

Q292.What is the class level lock? Explain its purpose. 
Ans. Every class in java has a unique lock if a Thread wants to execute static synchronized 
method that Thread has to get class level lock once a Thread got class level lock then only it is allow to execute static synchronized method. 
While a Thread executing any static synchronized method then remaining Threads are not allow to execute any static synchronized method of the same class simultaneously. But the remaining Threads are allow to execute the following method simultaneously: 
1. Any static non-synchronized method. 
2. Synchronized instance methods 
3. Non-synchronized instance method. 
There is no relationship between object lock and class level lock, both are independent. 

Q293. While a thread executing any synchronized method on the given object is it possible toexecute remaining synchronized method of the same object simultaneously by any other thread?
Ans. No, it is no possible.

Q294. What is the difference between synchronized method and static synchronized method? 
Ans. If a Thread wants to execute a synchronized method first it has to get the lock of the 
object. Once a Thread got the lock then it is allow to execute any synchronized method on that 
object.If a Thread wants to execute static synchronized method that Thread has to get class 
level lock once a Thread got class level lock then only it is allow to execute static synchronized method. 

Q295. What is the advantage of synchronized block over synchronized method? 
Ans. If very few lines of the code required synchronization then declaring entire method as the synchronized is not recommended. We have to declare those few lines of the code inside 
synchronized block. This approach reduces waiting time of the Thread and improves performance of the system. 

Q296. What is synchronized statement? 
Ans. The Statement which is inside the synchronized area (synchronized method or synchronized block) is called synchronized statement. 

Q297. How we can declare synchronized block to get class level lock? 
Ans. To get the class level lock we can declare synchronized block as follows: 
synchronized(Display.class) 



Q298. How two thread will communicate with each other? 
Ans. Two Threads will communicate with each other by using wait(), notify(), notifyAll() methods. 

Q299.wait(), notify(), notifyAll() method can available in which class? 
Ans. These methods are defined in Object class. 

Q300.Why wait(), notify(), notifyAll() method defines in object class instead of Thread class? 
Ans. These methods are defined in Object class but not in Thread because Threads are calling this method on the shared object. 

No comments:

Post a Comment