< prev index next >

test/jdk/java/lang/management/ThreadMXBean/VirtualThreads.java

Print this page
*** 76,12 ***
              ThreadInfo[] infos = bean.dumpAllThreads(false, false, maxDepth);
              Set<Long> tids = Arrays.stream(infos)
                      .map(ThreadInfo::getThreadId)
                      .collect(Collectors.toSet());
  
!             // current thread should be included
!             assertTrue(tids.contains(Thread.currentThread().threadId()));
  
              // virtual thread should not be included
              assertFalse(tids.contains(vthread.threadId()));
          } finally {
              LockSupport.unpark(vthread);
--- 76,13 ---
              ThreadInfo[] infos = bean.dumpAllThreads(false, false, maxDepth);
              Set<Long> tids = Arrays.stream(infos)
                      .map(ThreadInfo::getThreadId)
                      .collect(Collectors.toSet());
  
!             // if current thread is a platform thread then it should be included
!             boolean expected = !Thread.currentThread().isVirtual();
+             assertEquals(expected, tids.contains(Thread.currentThread().threadId()));
  
              // virtual thread should not be included
              assertFalse(tids.contains(vthread.threadId()));
          } finally {
              LockSupport.unpark(vthread);

*** 95,13 ***
      void testGetAllThreadIds() {
          Thread vthread = Thread.startVirtualThread(LockSupport::park);
          try {
              long[] tids = ManagementFactory.getThreadMXBean().getAllThreadIds();
  
!             // current thread should be included
              long currentTid = Thread.currentThread().threadId();
!             assertTrue(Arrays.stream(tids).anyMatch(tid -> tid == currentTid));
  
              // virtual thread should not be included
              long vtid = vthread.threadId();
              assertFalse(Arrays.stream(tids).anyMatch(tid -> tid == vtid));
          } finally {
--- 96,14 ---
      void testGetAllThreadIds() {
          Thread vthread = Thread.startVirtualThread(LockSupport::park);
          try {
              long[] tids = ManagementFactory.getThreadMXBean().getAllThreadIds();
  
!             // if current thread is a platform thread then it should be included
+             boolean expected = !Thread.currentThread().isVirtual();
              long currentTid = Thread.currentThread().threadId();
!             assertEquals(expected, Arrays.stream(tids).anyMatch(tid -> tid == currentTid));
  
              // virtual thread should not be included
              long vtid = vthread.threadId();
              assertFalse(Arrays.stream(tids).anyMatch(tid -> tid == vtid));
          } finally {

*** 151,11 ***
          try {
              long tid0 = Thread.currentThread().threadId();
              long tid1 = vthread.threadId();
              long[] tids = new long[] { tid0, tid1 };
              ThreadInfo[] infos = ManagementFactory.getThreadMXBean().getThreadInfo(tids, maxDepth);
!             assertEquals(tid0, infos[0].getThreadId());
              assertNull(infos[1]);
          } finally {
              LockSupport.unpark(vthread);
          }
      }
--- 153,15 ---
          try {
              long tid0 = Thread.currentThread().threadId();
              long tid1 = vthread.threadId();
              long[] tids = new long[] { tid0, tid1 };
              ThreadInfo[] infos = ManagementFactory.getThreadMXBean().getThreadInfo(tids, maxDepth);
!             if (Thread.currentThread().isVirtual()) {
+                 assertNull(infos[0]);
+             } else {
+                 assertEquals(tid0, infos[0].getThreadId());
+             }
              assertNull(infos[1]);
          } finally {
              LockSupport.unpark(vthread);
          }
      }

*** 172,11 ***
              long tid0 = Thread.currentThread().threadId();
              long tid1 = vthread.threadId();
              long[] tids = new long[] { tid0, tid1 };
              ThreadMXBean bean = ManagementFactory.getThreadMXBean();
              ThreadInfo[] infos = bean.getThreadInfo(tids, false, false, maxDepth);
!             assertEquals(tid0, infos[0].getThreadId());
              assertNull(infos[1]);
          } finally {
              LockSupport.unpark(vthread);
          }
      }
--- 178,15 ---
              long tid0 = Thread.currentThread().threadId();
              long tid1 = vthread.threadId();
              long[] tids = new long[] { tid0, tid1 };
              ThreadMXBean bean = ManagementFactory.getThreadMXBean();
              ThreadInfo[] infos = bean.getThreadInfo(tids, false, false, maxDepth);
!             if (Thread.currentThread().isVirtual()) {
+                 assertNull(infos[0]);
+             } else {
+                 assertEquals(tid0, infos[0].getThreadId());
+             }
              assertNull(infos[1]);
          } finally {
              LockSupport.unpark(vthread);
          }
      }
< prev index next >