< prev index next >

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

Print this page




 907      * @see        ThreadDeath
 908      * @see        ThreadGroup#uncaughtException(Thread,Throwable)
 909      * @see        SecurityManager#checkAccess(Thread)
 910      * @see        SecurityManager#checkPermission
 911      * @deprecated This method is inherently unsafe.  Stopping a thread with
 912      *       Thread.stop causes it to unlock all of the monitors that it
 913      *       has locked (as a natural consequence of the unchecked
 914      *       {@code ThreadDeath} exception propagating up the stack).  If
 915      *       any of the objects previously protected by these monitors were in
 916      *       an inconsistent state, the damaged objects become visible to
 917      *       other threads, potentially resulting in arbitrary behavior.  Many
 918      *       uses of {@code stop} should be replaced by code that simply
 919      *       modifies some variable to indicate that the target thread should
 920      *       stop running.  The target thread should check this variable
 921      *       regularly, and return from its run method in an orderly fashion
 922      *       if the variable indicates that it is to stop running.  If the
 923      *       target thread waits for long periods (on a condition variable,
 924      *       for example), the {@code interrupt} method should be used to
 925      *       interrupt the wait.
 926      *       For more information, see
 927      *       <a href="{@docRoot}/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
 928      *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 929      */
 930     @Deprecated(since="1.2")
 931     public final void stop() {
 932         SecurityManager security = System.getSecurityManager();
 933         if (security != null) {
 934             checkAccess();
 935             if (this != Thread.currentThread()) {
 936                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 937             }
 938         }
 939         // A zero status value corresponds to "NEW", it can't change to
 940         // not-NEW because we hold the lock.
 941         if (threadStatus != 0) {
 942             resume(); // Wake up thread if it was suspended; no-op otherwise
 943         }
 944 
 945         // The VM can handle all thread states
 946         stop0(new ThreadDeath());
 947     }
 948 
 949     /**
 950      * Throws {@code UnsupportedOperationException}.
 951      *
 952      * @param obj ignored
 953      *
 954      * @deprecated This method was originally designed to force a thread to stop
 955      *        and throw a given {@code Throwable} as an exception. It was
 956      *        inherently unsafe (see {@link #stop()} for details), and furthermore
 957      *        could be used to generate exceptions that the target thread was
 958      *        not prepared to handle.
 959      *        For more information, see
 960      *        <a href="{@docRoot}/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
 961      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 962      *        This method is subject to removal in a future version of Java SE.
 963      */
 964     @Deprecated(since="1.2", forRemoval=true)
 965     public final synchronized void stop(Throwable obj) {
 966         throw new UnsupportedOperationException();
 967     }
 968 
 969     /**
 970      * Interrupts this thread.
 971      *
 972      * <p> Unless the current thread is interrupting itself, which is
 973      * always permitted, the {@link #checkAccess() checkAccess} method
 974      * of this thread is invoked, which may cause a {@link
 975      * SecurityException} to be thrown.
 976      *
 977      * <p> If this thread is blocked in an invocation of the {@link
 978      * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 979      * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 980      * class, or of the {@link #join()}, {@link #join(long)}, {@link


1066      * Tests if some Thread has been interrupted.  The interrupted state
1067      * is reset or not based on the value of ClearInterrupted that is
1068      * passed.
1069      */
1070     @HotSpotIntrinsicCandidate
1071     private native boolean isInterrupted(boolean ClearInterrupted);
1072 
1073     /**
1074      * Throws {@link NoSuchMethodError}.
1075      *
1076      * @deprecated This method was originally designed to destroy this
1077      *     thread without any cleanup. Any monitors it held would have
1078      *     remained locked. However, the method was never implemented.
1079      *     If it were to be implemented, it would be deadlock-prone in
1080      *     much the manner of {@link #suspend}. If the target thread held
1081      *     a lock protecting a critical system resource when it was
1082      *     destroyed, no thread could ever access this resource again.
1083      *     If another thread ever attempted to lock this resource, deadlock
1084      *     would result. Such deadlocks typically manifest themselves as
1085      *     "frozen" processes. For more information, see
1086      *     <a href="{@docRoot}/java/lang/doc-files/threadPrimitiveDeprecation.html">
1087      *     Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1088      *     This method is subject to removal in a future version of Java SE.
1089      * @throws NoSuchMethodError always
1090      */
1091     @Deprecated(since="1.5", forRemoval=true)
1092     public void destroy() {
1093         throw new NoSuchMethodError();
1094     }
1095 
1096     /**
1097      * Tests if this thread is alive. A thread is alive if it has
1098      * been started and has not yet died.
1099      *
1100      * @return  {@code true} if this thread is alive;
1101      *          {@code false} otherwise.
1102      */
1103     public final native boolean isAlive();
1104 
1105     /**
1106      * Suspends this thread.
1107      * <p>
1108      * First, the {@code checkAccess} method of this thread is called
1109      * with no arguments. This may result in throwing a
1110      * {@code SecurityException }(in the current thread).
1111      * <p>
1112      * If the thread is alive, it is suspended and makes no further
1113      * progress unless and until it is resumed.
1114      *
1115      * @throws     SecurityException  if the current thread cannot modify
1116      *             this thread.
1117      * @see #checkAccess
1118      * @deprecated   This method has been deprecated, as it is
1119      *   inherently deadlock-prone.  If the target thread holds a lock on the
1120      *   monitor protecting a critical system resource when it is suspended, no
1121      *   thread can access this resource until the target thread is resumed. If
1122      *   the thread that would resume the target thread attempts to lock this
1123      *   monitor prior to calling {@code resume}, deadlock results.  Such
1124      *   deadlocks typically manifest themselves as "frozen" processes.
1125      *   For more information, see
1126      *   <a href="{@docRoot}/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
1127      *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1128      */
1129     @Deprecated(since="1.2")
1130     public final void suspend() {
1131         checkAccess();
1132         suspend0();
1133     }
1134 
1135     /**
1136      * Resumes a suspended thread.
1137      * <p>
1138      * First, the {@code checkAccess} method of this thread is called
1139      * with no arguments. This may result in throwing a
1140      * {@code SecurityException} (in the current thread).
1141      * <p>
1142      * If the thread is alive but suspended, it is resumed and is
1143      * permitted to make progress in its execution.
1144      *
1145      * @throws     SecurityException  if the current thread cannot modify this
1146      *             thread.
1147      * @see        #checkAccess
1148      * @see        #suspend()
1149      * @deprecated This method exists solely for use with {@link #suspend},
1150      *     which has been deprecated because it is deadlock-prone.
1151      *     For more information, see
1152      *     <a href="{@docRoot}/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
1153      *     are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1154      */
1155     @Deprecated(since="1.2")
1156     public final void resume() {
1157         checkAccess();
1158         resume0();
1159     }
1160 
1161     /**
1162      * Changes the priority of this thread.
1163      * <p>
1164      * First the {@code checkAccess} method of this thread is called
1165      * with no arguments. This may result in throwing a {@code SecurityException}.
1166      * <p>
1167      * Otherwise, the priority of this thread is set to the smaller of
1168      * the specified {@code newPriority} and the maximum permitted
1169      * priority of the thread's thread group.
1170      *
1171      * @param newPriority priority to set this thread to
1172      * @throws     IllegalArgumentException  If the priority is not in the




 907      * @see        ThreadDeath
 908      * @see        ThreadGroup#uncaughtException(Thread,Throwable)
 909      * @see        SecurityManager#checkAccess(Thread)
 910      * @see        SecurityManager#checkPermission
 911      * @deprecated This method is inherently unsafe.  Stopping a thread with
 912      *       Thread.stop causes it to unlock all of the monitors that it
 913      *       has locked (as a natural consequence of the unchecked
 914      *       {@code ThreadDeath} exception propagating up the stack).  If
 915      *       any of the objects previously protected by these monitors were in
 916      *       an inconsistent state, the damaged objects become visible to
 917      *       other threads, potentially resulting in arbitrary behavior.  Many
 918      *       uses of {@code stop} should be replaced by code that simply
 919      *       modifies some variable to indicate that the target thread should
 920      *       stop running.  The target thread should check this variable
 921      *       regularly, and return from its run method in an orderly fashion
 922      *       if the variable indicates that it is to stop running.  If the
 923      *       target thread waits for long periods (on a condition variable,
 924      *       for example), the {@code interrupt} method should be used to
 925      *       interrupt the wait.
 926      *       For more information, see
 927      *       <a href="{@docRoot}/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
 928      *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 929      */
 930     @Deprecated(since="1.2")
 931     public final void stop() {
 932         SecurityManager security = System.getSecurityManager();
 933         if (security != null) {
 934             checkAccess();
 935             if (this != Thread.currentThread()) {
 936                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 937             }
 938         }
 939         // A zero status value corresponds to "NEW", it can't change to
 940         // not-NEW because we hold the lock.
 941         if (threadStatus != 0) {
 942             resume(); // Wake up thread if it was suspended; no-op otherwise
 943         }
 944 
 945         // The VM can handle all thread states
 946         stop0(new ThreadDeath());
 947     }
 948 
 949     /**
 950      * Throws {@code UnsupportedOperationException}.
 951      *
 952      * @param obj ignored
 953      *
 954      * @deprecated This method was originally designed to force a thread to stop
 955      *        and throw a given {@code Throwable} as an exception. It was
 956      *        inherently unsafe (see {@link #stop()} for details), and furthermore
 957      *        could be used to generate exceptions that the target thread was
 958      *        not prepared to handle.
 959      *        For more information, see
 960      *        <a href="{@docRoot}/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
 961      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 962      *        This method is subject to removal in a future version of Java SE.
 963      */
 964     @Deprecated(since="1.2", forRemoval=true)
 965     public final synchronized void stop(Throwable obj) {
 966         throw new UnsupportedOperationException();
 967     }
 968 
 969     /**
 970      * Interrupts this thread.
 971      *
 972      * <p> Unless the current thread is interrupting itself, which is
 973      * always permitted, the {@link #checkAccess() checkAccess} method
 974      * of this thread is invoked, which may cause a {@link
 975      * SecurityException} to be thrown.
 976      *
 977      * <p> If this thread is blocked in an invocation of the {@link
 978      * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 979      * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 980      * class, or of the {@link #join()}, {@link #join(long)}, {@link


1066      * Tests if some Thread has been interrupted.  The interrupted state
1067      * is reset or not based on the value of ClearInterrupted that is
1068      * passed.
1069      */
1070     @HotSpotIntrinsicCandidate
1071     private native boolean isInterrupted(boolean ClearInterrupted);
1072 
1073     /**
1074      * Throws {@link NoSuchMethodError}.
1075      *
1076      * @deprecated This method was originally designed to destroy this
1077      *     thread without any cleanup. Any monitors it held would have
1078      *     remained locked. However, the method was never implemented.
1079      *     If it were to be implemented, it would be deadlock-prone in
1080      *     much the manner of {@link #suspend}. If the target thread held
1081      *     a lock protecting a critical system resource when it was
1082      *     destroyed, no thread could ever access this resource again.
1083      *     If another thread ever attempted to lock this resource, deadlock
1084      *     would result. Such deadlocks typically manifest themselves as
1085      *     "frozen" processes. For more information, see
1086      *     <a href="{@docRoot}/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html">
1087      *     Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1088      *     This method is subject to removal in a future version of Java SE.
1089      * @throws NoSuchMethodError always
1090      */
1091     @Deprecated(since="1.5", forRemoval=true)
1092     public void destroy() {
1093         throw new NoSuchMethodError();
1094     }
1095 
1096     /**
1097      * Tests if this thread is alive. A thread is alive if it has
1098      * been started and has not yet died.
1099      *
1100      * @return  {@code true} if this thread is alive;
1101      *          {@code false} otherwise.
1102      */
1103     public final native boolean isAlive();
1104 
1105     /**
1106      * Suspends this thread.
1107      * <p>
1108      * First, the {@code checkAccess} method of this thread is called
1109      * with no arguments. This may result in throwing a
1110      * {@code SecurityException }(in the current thread).
1111      * <p>
1112      * If the thread is alive, it is suspended and makes no further
1113      * progress unless and until it is resumed.
1114      *
1115      * @throws     SecurityException  if the current thread cannot modify
1116      *             this thread.
1117      * @see #checkAccess
1118      * @deprecated   This method has been deprecated, as it is
1119      *   inherently deadlock-prone.  If the target thread holds a lock on the
1120      *   monitor protecting a critical system resource when it is suspended, no
1121      *   thread can access this resource until the target thread is resumed. If
1122      *   the thread that would resume the target thread attempts to lock this
1123      *   monitor prior to calling {@code resume}, deadlock results.  Such
1124      *   deadlocks typically manifest themselves as "frozen" processes.
1125      *   For more information, see
1126      *   <a href="{@docRoot}/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
1127      *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1128      */
1129     @Deprecated(since="1.2")
1130     public final void suspend() {
1131         checkAccess();
1132         suspend0();
1133     }
1134 
1135     /**
1136      * Resumes a suspended thread.
1137      * <p>
1138      * First, the {@code checkAccess} method of this thread is called
1139      * with no arguments. This may result in throwing a
1140      * {@code SecurityException} (in the current thread).
1141      * <p>
1142      * If the thread is alive but suspended, it is resumed and is
1143      * permitted to make progress in its execution.
1144      *
1145      * @throws     SecurityException  if the current thread cannot modify this
1146      *             thread.
1147      * @see        #checkAccess
1148      * @see        #suspend()
1149      * @deprecated This method exists solely for use with {@link #suspend},
1150      *     which has been deprecated because it is deadlock-prone.
1151      *     For more information, see
1152      *     <a href="{@docRoot}/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html">Why
1153      *     are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
1154      */
1155     @Deprecated(since="1.2")
1156     public final void resume() {
1157         checkAccess();
1158         resume0();
1159     }
1160 
1161     /**
1162      * Changes the priority of this thread.
1163      * <p>
1164      * First the {@code checkAccess} method of this thread is called
1165      * with no arguments. This may result in throwing a {@code SecurityException}.
1166      * <p>
1167      * Otherwise, the priority of this thread is set to the smaller of
1168      * the specified {@code newPriority} and the maximum permitted
1169      * priority of the thread's thread group.
1170      *
1171      * @param newPriority priority to set this thread to
1172      * @throws     IllegalArgumentException  If the priority is not in the


< prev index next >