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

TerminatingThreadLocal(s) not registered from virtual thread(s)

XMLWordPrintable

    • b10
    • Verified

        TerminatingThreadLocal is a JDK internal variant of ThreadLocal wihth a cleanup action to be called when a thread with bound value for a particular TerminatingThreadLocal instance is terminating. It is used internaly in JDK for prompt cleanup of thread-local caches of native ByteBuffer(s) and NativeBuffer(s). TerminatingThreadLocal is only ever used in the context of carrier threads. If a virtual thread is accessing a value, it is actually its currently bound carrier thread that is selected to hold the value. But if only virtual threads are creating the value(s) such TerminatingThreadLocal(s) are never registered and cleanup action is not called when the carrier thread exits:

        ```
            public static void register(TerminatingThreadLocal<?> tl) {
                if (!Thread.currentThread().isVirtual())
                    REGISTRY.get().add(tl);
            }
        ```

        You may say that carrier threads live forever, but that's not true. When the "virtual thread scheduler" (ForkJoinPool) is compensating for a pinned carrier thread that blocks, new carrier thread is created. After keep-alive period (30 seconds currently) of inactivity, such excessive carrier threads are terminated.

              plevart Peter Levart
              plevart Peter Levart
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: