Search your topic in Java2share
Home > FAQs
eXTReMe Tracker


Multi Threading FAQs


Untitled Document

Q33. 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.)

Q34.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.
                               
Q35. While a thread executing any synchronized method on the given object is it possible to execute remaining synchronized method of the same object simultaneously by any other thread?
Ans.       No, it is no possible.
Q36. 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.