I need to clear some points regarding synchronization at block level.
suppose following synchronization blocks are in a same method of a class:
class A{
some_method(){
//BLOCK1
synchronized(OBJ1){
shared code...
}
//BLOCK2
synchronized(OBJ1){
shared code...
}
//BLOCK3
synchronized(OBJ2){
shared code...
}
}
}
Following queries:
1) IS this right that if one thread enters block1 by obtaining lock on OBJ1, then no other thread can enter into Block2 as long as first thread holds lock on OBJ1, but other thread can run block3 simultaneously ?
2)Does first thread need to obtain lock on OBJ1 again if enters block2 after completing block1?
3)if first thread calls some code in block2, from block1 then will it have to release the lock on OBJ1 obtain during first block execution and again obtain it OR same lock obtain during first block will work?
Now suppose block1 is in a method of one class and blockk2 and 3 are in a method of some other class
4)Same as in point 1 holds true as synchronization is happening by obtaining lock on some third class obj (OBJ1,OBJ2)?
1 Answer