Is @onMethodEnter and @onMethodExit syncronized?

For example, I instrument a method foo which is implemented in a certain way so that only 1 thread at the time will call it.

If I instrument foo with onMethodEnter and onMethodExit, will onMethodExit always finish before the next onMethodEnter is called? Or can it be that even though foo is synchronized, that an onMethodEnter might be called before onMethodExit is finished executing?

Hi,

What the agent does it to inject code at the beginning and end of method body, thus if the method is synchronized (on the method signature).

// original method
public synchronized void foo(){
  // method body
}

// instrumented method
public synchronized void foo(){
  // call to onMethodEnter
  try {
    // method body
  } finally {
    // call to onMethodExit
  }
}

In this situation, onMethodEnter will always be executed before onMethodExit.

But that would not be the case if the original method body is using an inner synchronized block.

// instrumented method
public void foo(){
  // call to onMethodEnter
  try {
    synchronized(this){
      // method body
    }
  } finally {
    // call to onMethodExit
  }
}
1 Like

Thanks for the clean explanation! I will have to dig deeper into the foo method then on how exactly it achieves the synchronized behaviour.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.