Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8305425

Thread.isAlive0 doesn't need to call into the VM

    XMLWordPrintable

Details

    • b17

    Backports

      Description

        We have the strange situation where calling t.isAlive() on a java.lang.Thread t, will call into the VM (via alive() then isAlive0()) where the VM then examines the eetop field of t to extract its JavaThread pointer and compare it to null. We can simply read eetop directly in Thread.alive():

        synchronized boolean alive() {
          return eetop != 0;
        }

        Why the VM call was ever performed is lost in history. In JDK 1.1 the code was completely different. In JDK 1.2 we have essentially the same code as today.

        The synchronized is needed to ensure thread terminations Happens-before isAlive() returns false. And it also guards against the field check being hoisted in a loop like: while (t.isAlive() ...

        EDIT: The actual fix used a volatile eetop rather than synchronized in isAlive. The VM also treats eetop like a volatile and does the necessary `release` operation to create the required happens-before ordering.

        Attachments

          Issue Links

            Activity

              People

                dholmes David Holmes
                dholmes David Holmes
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: