<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>We can discover deadlocks without a thread dump using the
      ThreadMXBean, but currently that does not work for virtual
      threads.<br>
    </p>
    <pre class="moz-signature" cols="72">Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java™ Specialists' Newsletter" - <a class="moz-txt-link-abbreviated" href="http://www.javaspecialists.eu">www.javaspecialists.eu</a>
Java Champion - <a class="moz-txt-link-abbreviated" href="http://www.javachampions.org">www.javachampions.org</a>
JavaOne Rock Star Speaker
Tel: +30 69 75 595 262
Skype: kabutz
</pre>
    <div class="moz-cite-prefix">On 2024-11-20 09:12, Alex Otenko wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CANkgWKiGDQEJn28TN_aF89aXhhtG9JxKh7dnaSu38Zb1qb9cNg@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <p dir="ltr">By the way, deadlocks specifically should be
        discoverable automatically without a thread dump.</p>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">On Wed, 20 Nov 2024, 07:10 Dr
          Heinz M. Kabutz, <<a href="mailto:heinz@javaspecialists.eu"
            moz-do-not-send="true" class="moz-txt-link-freetext">heinz@javaspecialists.eu</a>>
          wrote:<br>
        </div>
        <blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">With
          platform threads, we have had good techniques for finding
          threading <br>
          issues. For example, a thread dump would reveal deadlocks,
          livelocks and <br>
          contention.<br>
          <br>
          However, with virtual threads, most of these techniques won't
          work <br>
          anymore. For example, here is a SimpleDeadlock:<br>
          <br>
          import java.util.concurrent.locks.*;<br>
          <br>
          public class SimpleDeadlock {<br>
               public static void main(String... args) throws
          InterruptedException {<br>
                   var monitor1 = new Object();<br>
                   var monitor2 = new Object();<br>
                   Thread.startVirtualThread(() -> {<br>
                       synchronized (monitor1) {<br>
                           LockSupport.parkNanos(10_000_000);<br>
                           synchronized (monitor2) {<br>
                               System.out.println("Got both locks");<br>
                           }<br>
                       }<br>
                   });<br>
                   Thread.startVirtualThread(() -> {<br>
                       synchronized (monitor2) {<br>
                           LockSupport.parkNanos(10_000_000);<br>
                           synchronized (monitor1) {<br>
                               System.out.println("Got both locks");<br>
                           }<br>
                       }<br>
                   }).join();<br>
               }<br>
          }<br>
          <br>
          If we run this with Java 21-23 and
          -Djdk.trackAllThreads=false, the <br>
          carrier thread indicate that they are carrying some virtual
          threads, but <br>
          these do not appear in the full dump. In Java 24+24, the
          virtual threads <br>
          are parked instead of pinned, and thus they vanish completely
          from the <br>
          thread dumps.<br>
          <br>
          Is there any project or workgroup where debugging of virtual
          threads is <br>
          being looked at?<br>
          <br>
          Regards<br>
          <br>
          Heinz<br>
          -- <br>
          Dr Heinz M. Kabutz (PhD CompSci)<br>
          Author of "The Java™ Specialists' Newsletter" - <a
            href="http://www.javaspecialists.eu"
            rel="noreferrer noreferrer" target="_blank"
            moz-do-not-send="true">www.javaspecialists.eu</a><br>
          Java Champion - <a href="http://www.javachampions.org"
            rel="noreferrer noreferrer" target="_blank"
            moz-do-not-send="true">www.javachampions.org</a><br>
          JavaOne Rock Star Speaker<br>
          Tel: +30 69 75 595 262<br>
          Skype: kabutz<br>
          <br>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>