# Is @onMethodEnter and @onMethodExit syncronized?

**URL:** <https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016>\
**Category:** APM\
**Tags:** java\
**Created:** [July 29, 2021, 8:34pm UTC](https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016 "2021-07-29T20:34:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milanvdm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milanvdm/32/82732_2.png) [@milanvdm](https://discuss.elastic.co/u/milanvdm)\
**Post date:** [July 29, 2021, 8:34pm UTC](https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016/1 "2021-07-29T20:34:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [July 30, 2021, 6:57am UTC](https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016/2 "2021-07-30T06:57:55Z")

</div>

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).

```java
// 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.

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

```

---

<div class="post-metadata">

**Author:** ![milanvdm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milanvdm/32/82732_2.png) [@milanvdm](https://discuss.elastic.co/u/milanvdm)\
**Post date:** [July 30, 2021, 7:37am UTC](https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016/3 "2021-07-30T07:37:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 20, 2021, 8:43am UTC](https://discuss.elastic.co/t/is-onmethodenter-and-onmethodexit-syncronized/280016/5 "2021-08-20T08:43:53Z")

</div>

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