< prev index next >

src/java.base/share/classes/jdk/internal/vm/ThreadSnapshot.java

Print this page
*** 24,15 ***
   */
  package jdk.internal.vm;
  
  import java.util.Arrays;
  import java.util.stream.Stream;
  
  /**
   * Represents a snapshot of information about a Thread.
   */
! class ThreadSnapshot {
      private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0];
      private static final ThreadLock[] EMPTY_LOCKS = new ThreadLock[0];
  
      // filled by VM
      private String name;
--- 24,18 ---
   */
  package jdk.internal.vm;
  
  import java.util.Arrays;
  import java.util.stream.Stream;
+ import jdk.internal.access.JavaLangAccess;
+ import jdk.internal.access.SharedSecrets;
  
  /**
   * Represents a snapshot of information about a Thread.
   */
! public class ThreadSnapshot {
+     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
      private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0];
      private static final ThreadLock[] EMPTY_LOCKS = new ThreadLock[0];
  
      // filled by VM
      private String name;

*** 52,21 ***
  
      private ThreadSnapshot() {}
  
      /**
       * Take a snapshot of a Thread to get all information about the thread.
!      * Return null if a ThreadSnapshot is not created, for example if the
!      * thread has terminated.
!      * @throws UnsupportedOperationException if not supported by VM
       */
!     static ThreadSnapshot of(Thread thread) {
!         ThreadSnapshot snapshot = create(thread);
          if (snapshot == null) {
!             return null; // thread terminated
          }
          if (snapshot.stackTrace == null) {
              snapshot.stackTrace = EMPTY_STACK;
          }
          if (snapshot.locks != null) {
              Arrays.stream(snapshot.locks).forEach(ThreadLock::finishInit);
          } else {
              snapshot.locks = EMPTY_LOCKS;
--- 55,23 ---
  
      private ThreadSnapshot() {}
  
      /**
       * Take a snapshot of a Thread to get all information about the thread.
!      * @param thread the thread
!      * @param includeMonitors true to include the blocked on and owned object monitors
!      * @return the snapshot or {@code null} if the thread is not alive
       */
!     public static ThreadSnapshot of(Thread thread, boolean includeMonitors) {
!         ThreadSnapshot snapshot = create(thread, includeMonitors);
          if (snapshot == null) {
!             return null; // thread not alive
          }
          if (snapshot.stackTrace == null) {
              snapshot.stackTrace = EMPTY_STACK;
+         } else {
+             JLA.finishInit(snapshot.stackTrace);
          }
          if (snapshot.locks != null) {
              Arrays.stream(snapshot.locks).forEach(ThreadLock::finishInit);
          } else {
              snapshot.locks = EMPTY_LOCKS;

*** 96,11 ***
      }
  
      /**
       * Returns the thread stack trace.
       */
!     StackTraceElement[] stackTrace() {
          return stackTrace;
      }
  
      /**
       * Returns the thread's parkBlocker.
--- 101,11 ---
      }
  
      /**
       * Returns the thread stack trace.
       */
!     public StackTraceElement[] stackTrace() {
          return stackTrace;
      }
  
      /**
       * Returns the thread's parkBlocker.

*** 229,7 ***
          ThreadBlocker(int typeOrdinal, Object obj, Thread owner) {
              this(lockTypeValues[typeOrdinal], obj, owner);
          }
      }
  
!     private static native ThreadSnapshot create(Thread thread);
  }
--- 234,7 ---
          ThreadBlocker(int typeOrdinal, Object obj, Thread owner) {
              this(lockTypeValues[typeOrdinal], obj, owner);
          }
      }
  
!     private static native ThreadSnapshot create(Thread thread, boolean includeMonitors);
  }
< prev index next >