Post a Job  |   Ask a Question?   |   Already registered? login or register
srikanth reddy
22/05/2015 01:52 PM

what is the difference between object level lock and class level lock in multithreading?

any of them please answer in this...
Spam    1 Comments

22/05/2015 21:06:30
In java every object contains one lock and every class contains one lock. object level lock ========== if a thread wants to execute synchronized method [or] block on the given object ,first it has to get lock of that object .once thread got lock ,then it is allowed to execute any synchronized method on the given object.once synchronization method execution completes automatically thread releases the lock. while thread executing synchronized method on the given object,remaining threads are not allowed to execute any synchronized method.But remaing threads are allowed to execute non-synchronized methods on that same object Note:If multiple threads are operating on multiple objects then synchronization is not required.If multiple threads are operating on same object then synchronization is required. ex: we will perform like this class Employee { public synchronized void m1(){} public void m2(){} } Employee e=new Employee(); synchronize(e){ } here synchronized m1() is executed by one thread at a time.but non-synchronized m2() is executed by multipe threads at a time. class level lock ========== every class has a unique lock (classlevel lock) if thread wants to execute a static synchronization method then it has to get class level lock ,then it is allowed to execute any static synchronized method of that class. remaining lock concept same as object level lock class A { static synchronized m1(){} static synchronized {} static m3(){} } if we perform class level lock on class A as following below synchronized (A.class) { } then synchronized static block and synchronized static m1() are executed by one thread at a time .But non-synchronized static m3() is executed by multiple threads at a time. I HOPE IT IS CLEAR.

Languages :