1

According to JVM Specification, paragraph 3.14 Synchronization, JVM call monitorexit instruction twice.

I wonder why JVM need such behavior? Why do we call this instruction 2 times, but not 3 or 4 or N times?

May be it's somehow connected with types of synchronization locks?

Holger
  • 285,553
  • 42
  • 434
  • 765
Artiom Saidanov
  • 286
  • 1
  • 3
  • 13
  • 4
    Did you actually read the linked text? The code example is explained right beneath it; “*To enforce proper pairing of monitorenter and monitorexit instructions on abrupt method invocation completion, the compiler generates exception handlers (§2.10) that will match any exception and whose associated code executes the necessary monitorexit instructions.*” In contrast, a statement like “JVM call monitorexit instruction twice” does not appear anywhere in that document. – Holger Jun 12 '19 at 06:55

2 Answers2

3

The code is not "calling" the monitorexit instruction twice. It is executing it once on two different code paths.

  • The first codepath is for when the code in the synchronized block exits normally.
  • The second codepath is in an implicit exception handling path for the case where the block terminates abnormally.

You could write the bytecodes in the example as pseudo-code that looks like this:

void onlyMe(Foo f) {
    monitorEntry(f);

    try {
        doSomething();
        monitorExit();
    } catch (Throwable any) {
        monitorExit();
        throw any;
    }
}

If you want more information on this, take a look at this older question:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I have a related question. Why is there a second entry in the exception handler table? If monitorexit throws it will loop infinitely right? But if that doesn’t throw then that’s the purpose of that second entry? – tgeng Jun 18 '23 at 02:11
  • Please ask this as a new question. And before you do that, please read carefully what this Q and all of the As say. In your question, 1) provide evidence that there is a 2nd entry in the exception handler table, and 2) explain why do you think that the code can loop. – Stephen C Jun 18 '23 at 06:53
  • Ah I found another person asking this exact question https://stackoverflow.com/questions/18249906/how-do-i-exit-a-monitor-in-bytecode-properly – tgeng Jun 19 '23 at 04:44
  • I answered it too. 10 years ago. Fancy that! – Stephen C Jun 19 '23 at 06:16
1

If you look at the code, it should ends after line 10 where it goto return statement

9   monitorexit         // Exit the monitor associated with f
10  goto 18             // Complete the method normally
13  astore_3            // In case of any throw, end up here
14  aload_2             // Push local variable 2 (f)
15  monitorexit         // Be sure to exit the monitor!
16  aload_3             // Push thrown value...
17  athrow              // ...and rethrow value to the invoker
18  return              // Return in the normal case

But it adds a extra caution check if failed to return call again monitorexit

Ori Marko
  • 56,308
  • 23
  • 131
  • 233