Wednesday, April 14, 2010

More Synchronized funda from another SCJP

Nowadays I wonder whether synchronized is a keyword of JAVA or not. Because if it were, it would have been clear to people appearing for interview especially those who are SCJP.

Since two threads can't execute different synchronized method at a time. A lock on object is acquired to achieve this. Fair enough, that's what is written in books. Can a synchronized method call another synchronized method ? Consider below example ... can methodA call methodB where both are synchronized ?

class A{

public synchronized void methodA(){

//some operations 
    methodB();
}

public synchronized void methodB(){
//some operations 
}

}

What do you think ? 

Well this is answer from SCJP .... Since lock is already acquired methodB will not be able to acquire it again. It would be blocked sort of deadlock. !!!! 

No comments:

Post a Comment