>> Tuesday, June 24, 2014
Q. What does reentrancy mean regarding intrinsic or explcit locks?
A. Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis.
Example :
public synchronized void method1(){
//intrinsic lock is acquired
operation1(); //ok to enter this synchronized method
//as locks are on per thread basis
operation2(); //ok to enter this synchronized method
//as locks are on per thread basis
//intrinsic lock is released
}
public synchronized void operation1(){
//process 1
}
public synchronized void operation2(){
//process 1
}
In Java, both intrinsic and explicit locks are re-entrant.
Q. If 2 different threads hit 2 different synchronized methods in an object at the same time will they both continue?
A. No. Only one thread can acquire the lock in a synchronized method of an object. Each object has a synchronization lock. No 2 synchronized methods within an object can run at the same time. One synchronized method should wait for the other synchronized method to release the lock. This is demonstrated here with method level lock. Same concept is applicable for block level locks as well.
A. Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis.
Example :
public synchronized void method1(){
//intrinsic lock is acquired
operation1(); //ok to enter this synchronized method
//as locks are on per thread basis
operation2(); //ok to enter this synchronized method
//as locks are on per thread basis
//intrinsic lock is released
}
public synchronized void operation1(){
//process 1
}
public synchronized void operation2(){
//process 1
}
In Java, both intrinsic and explicit locks are re-entrant.
Q. If 2 different threads hit 2 different synchronized methods in an object at the same time will they both continue?
A. No. Only one thread can acquire the lock in a synchronized method of an object. Each object has a synchronization lock. No 2 synchronized methods within an object can run at the same time. One synchronized method should wait for the other synchronized method to release the lock. This is demonstrated here with method level lock. Same concept is applicable for block level locks as well.
0 comments:
Post a Comment