dimanche 26 mars 2017

How implementation of Re-entrant lock and Synchronization is different

As per java source code

ReentrantLock's lock(non-fair) is as below.

public boolean lock(){
      int c=getState();
      if(c==0){
         compareAndSetState(0,1);
      }
}
//getState method
public int getState(){
    return state;
}
public boolean compareAndSetState(int expected,int update){
    unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}

and value of stateOffSet is offset of state in memory(using reflection).

what i can understand from above code is ,when we call lock.lock(),first value of state fields in checked in memory,if it is zero then only lock is given to calling thread.

so my question is what happens when we user Synchronized key word? does in Synchronized also some field of lock is checked and set?

one more doubt,somewhere I read reentrant lock allows multiple wait queue,what does that mean?don't we have just one queue for both?





Aucun commentaire:

Enregistrer un commentaire