< prev index next >

jdk/src/java.base/share/classes/java/lang/Thread.java

Print this page




1312      *
1313      * <p> An invocation of this method behaves in exactly the same
1314      * way as the invocation
1315      *
1316      * <blockquote>
1317      * {@linkplain #join(long) join}{@code (0)}
1318      * </blockquote>
1319      *
1320      * @throws  InterruptedException
1321      *          if any thread has interrupted the current thread. The
1322      *          <i>interrupted status</i> of the current thread is
1323      *          cleared when this exception is thrown.
1324      */
1325     public final void join() throws InterruptedException {
1326         join(0);
1327     }
1328 
1329     /**
1330      * Prints a stack trace of the current thread to the standard error stream.
1331      * This method is used only for debugging.
1332      *
1333      * @see     Throwable#printStackTrace()
1334      */
1335     public static void dumpStack() {
1336         new Exception("Stack trace").printStackTrace();
1337     }
1338 
1339     /**
1340      * Marks this thread as either a {@linkplain #isDaemon daemon} thread
1341      * or a user thread. The Java Virtual Machine exits when the only
1342      * threads running are all daemon threads.
1343      *
1344      * <p> This method must be invoked before the thread is started.
1345      *
1346      * @param  on
1347      *         if {@code true}, marks this thread as a daemon thread
1348      *
1349      * @throws  IllegalThreadStateException
1350      *          if this thread is {@linkplain #isAlive alive}
1351      *
1352      * @throws  SecurityException
1353      *          if {@link #checkAccess} determines that the current
1354      *          thread cannot modify this thread
1355      */
1356     public final void setDaemon(boolean on) {


1539             SecurityManager security = System.getSecurityManager();
1540             if (security != null) {
1541                 security.checkPermission(
1542                     SecurityConstants.GET_STACK_TRACE_PERMISSION);
1543             }
1544             // optimization so we do not call into the vm for threads that
1545             // have not yet started or have terminated
1546             if (!isAlive()) {
1547                 return EMPTY_STACK_TRACE;
1548             }
1549             StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
1550             StackTraceElement[] stackTrace = stackTraceArray[0];
1551             // a thread that was alive during the previous isAlive call may have
1552             // since terminated, therefore not having a stacktrace.
1553             if (stackTrace == null) {
1554                 stackTrace = EMPTY_STACK_TRACE;
1555             }
1556             return stackTrace;
1557         } else {
1558             // Don't need JVM help for current thread
1559             return (new Exception()).getStackTrace();
1560         }
1561     }
1562 
1563     /**
1564      * Returns a map of stack traces for all live threads.
1565      * The map keys are threads and each map value is an array of
1566      * {@code StackTraceElement} that represents the stack dump
1567      * of the corresponding {@code Thread}.
1568      * The returned stack traces are in the format specified for
1569      * the {@link #getStackTrace getStackTrace} method.
1570      *
1571      * <p>The threads may be executing while this method is called.
1572      * The stack trace of each thread only represents a snapshot and
1573      * each stack trace may be obtained at different time.  A zero-length
1574      * array will be returned in the map value if the virtual machine has
1575      * no stack trace information about a thread.
1576      *
1577      * <p>If there is a security manager, then the security manager's
1578      * {@code checkPermission} method is called with a
1579      * {@code RuntimePermission("getStackTrace")} permission as well as




1312      *
1313      * <p> An invocation of this method behaves in exactly the same
1314      * way as the invocation
1315      *
1316      * <blockquote>
1317      * {@linkplain #join(long) join}{@code (0)}
1318      * </blockquote>
1319      *
1320      * @throws  InterruptedException
1321      *          if any thread has interrupted the current thread. The
1322      *          <i>interrupted status</i> of the current thread is
1323      *          cleared when this exception is thrown.
1324      */
1325     public final void join() throws InterruptedException {
1326         join(0);
1327     }
1328 
1329     /**
1330      * Prints a stack trace of the current thread to the standard error stream.
1331      * This method is used only for debugging.


1332      */
1333     public static void dumpStack() {
1334         StackStreamFactory.makeStackTrace().printStackTrace(System.err);
1335     }
1336 
1337     /**
1338      * Marks this thread as either a {@linkplain #isDaemon daemon} thread
1339      * or a user thread. The Java Virtual Machine exits when the only
1340      * threads running are all daemon threads.
1341      *
1342      * <p> This method must be invoked before the thread is started.
1343      *
1344      * @param  on
1345      *         if {@code true}, marks this thread as a daemon thread
1346      *
1347      * @throws  IllegalThreadStateException
1348      *          if this thread is {@linkplain #isAlive alive}
1349      *
1350      * @throws  SecurityException
1351      *          if {@link #checkAccess} determines that the current
1352      *          thread cannot modify this thread
1353      */
1354     public final void setDaemon(boolean on) {


1537             SecurityManager security = System.getSecurityManager();
1538             if (security != null) {
1539                 security.checkPermission(
1540                     SecurityConstants.GET_STACK_TRACE_PERMISSION);
1541             }
1542             // optimization so we do not call into the vm for threads that
1543             // have not yet started or have terminated
1544             if (!isAlive()) {
1545                 return EMPTY_STACK_TRACE;
1546             }
1547             StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
1548             StackTraceElement[] stackTrace = stackTraceArray[0];
1549             // a thread that was alive during the previous isAlive call may have
1550             // since terminated, therefore not having a stacktrace.
1551             if (stackTrace == null) {
1552                 stackTrace = EMPTY_STACK_TRACE;
1553             }
1554             return stackTrace;
1555         } else {
1556             // Don't need JVM help for current thread
1557             return StackStreamFactory.makeStackTrace().getStackTraceElements();
1558         }
1559     }
1560 
1561     /**
1562      * Returns a map of stack traces for all live threads.
1563      * The map keys are threads and each map value is an array of
1564      * {@code StackTraceElement} that represents the stack dump
1565      * of the corresponding {@code Thread}.
1566      * The returned stack traces are in the format specified for
1567      * the {@link #getStackTrace getStackTrace} method.
1568      *
1569      * <p>The threads may be executing while this method is called.
1570      * The stack trace of each thread only represents a snapshot and
1571      * each stack trace may be obtained at different time.  A zero-length
1572      * array will be returned in the map value if the virtual machine has
1573      * no stack trace information about a thread.
1574      *
1575      * <p>If there is a security manager, then the security manager's
1576      * {@code checkPermission} method is called with a
1577      * {@code RuntimePermission("getStackTrace")} permission as well as


< prev index next >