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

Print this page




 795      *       Thread.stop causes it to unlock all of the monitors that it
 796      *       has locked (as a natural consequence of the unchecked
 797      *       <code>ThreadDeath</code> exception propagating up the stack).  If
 798      *       any of the objects previously protected by these monitors were in
 799      *       an inconsistent state, the damaged objects become visible to
 800      *       other threads, potentially resulting in arbitrary behavior.  Many
 801      *       uses of <code>stop</code> should be replaced by code that simply
 802      *       modifies some variable to indicate that the target thread should
 803      *       stop running.  The target thread should check this variable
 804      *       regularly, and return from its run method in an orderly fashion
 805      *       if the variable indicates that it is to stop running.  If the
 806      *       target thread waits for long periods (on a condition variable,
 807      *       for example), the <code>interrupt</code> method should be used to
 808      *       interrupt the wait.
 809      *       For more information, see
 810      *       <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 811      *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 812      */
 813     @Deprecated
 814     public final void stop() {
 815         stop(new ThreadDeath());
 816     }
 817 
 818     /**
 819      * Forces the thread to stop executing.
 820      * <p>
 821      * If there is a security manager installed, the <code>checkAccess</code>
 822      * method of this thread is called, which may result in a
 823      * <code>SecurityException</code> being raised (in the current thread).
 824      * <p>
 825      * If this thread is different from the current thread (that is, the current
 826      * thread is trying to stop a thread other than itself) or
 827      * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
 828      * security manager's <code>checkPermission</code> method (with the
 829      * <code>RuntimePermission("stopThread")</code> argument) is called in
 830      * addition.
 831      * Again, this may result in throwing a
 832      * <code>SecurityException</code> (in the current thread).
 833      * <p>
 834      * If the argument <code>obj</code> is null, a
 835      * <code>NullPointerException</code> is thrown (in the current thread).
 836      * <p>
 837      * The thread represented by this thread is forced to stop
 838      * whatever it is doing abnormally and to throw the
 839      * <code>Throwable</code> object <code>obj</code> as an exception. This
 840      * is an unusual action to take; normally, the <code>stop</code> method
 841      * that takes no arguments should be used.
 842      * <p>
 843      * It is permitted to stop a thread that has not yet been started.
 844      * If the thread is eventually started, it immediately terminates.
 845      *
 846      * @param      obj   the Throwable object to be thrown.
 847      * @exception  SecurityException  if the current thread cannot modify
 848      *               this thread.
 849      * @throws     NullPointerException if obj is <tt>null</tt>.
 850      * @see        #interrupt()
 851      * @see        #checkAccess()
 852      * @see        #run()
 853      * @see        #start()
 854      * @see        #stop()
 855      * @see        SecurityManager#checkAccess(Thread)
 856      * @see        SecurityManager#checkPermission
 857      * @deprecated This method is inherently unsafe.  See {@link #stop()}
 858      *        for details.  An additional danger of this
 859      *        method is that it may be used to generate exceptions that the
 860      *        target thread is unprepared to handle (including checked
 861      *        exceptions that the thread could not possibly throw, were it
 862      *        not for this method).
 863      *        For more information, see
 864      *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 865      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 866      */
 867     @Deprecated
 868     public final synchronized void stop(Throwable obj) {
 869         if (obj == null)
 870             throw new NullPointerException();
 871 
 872         SecurityManager security = System.getSecurityManager();
 873         if (security != null) {
 874             checkAccess();
 875             if ((this != Thread.currentThread()) ||
 876                 (!(obj instanceof ThreadDeath))) {
 877                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 878             }
 879         }
 880         // A zero status value corresponds to "NEW", it can't change to
 881         // not-NEW because we hold the lock.
 882         if (threadStatus != 0) {
 883             resume(); // Wake up thread if it was suspended; no-op otherwise
 884         }
 885 
 886         // The VM can handle all thread states
 887         stop0(obj);



















 888     }
 889 
 890     /**
 891      * Interrupts this thread.
 892      *
 893      * <p> Unless the current thread is interrupting itself, which is
 894      * always permitted, the {@link #checkAccess() checkAccess} method
 895      * of this thread is invoked, which may cause a {@link
 896      * SecurityException} to be thrown.
 897      *
 898      * <p> If this thread is blocked in an invocation of the {@link
 899      * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 900      * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 901      * class, or of the {@link #join()}, {@link #join(long)}, {@link
 902      * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 903      * methods of this class, then its interrupt status will be cleared and it
 904      * will receive an {@link InterruptedException}.
 905      *
 906      * <p> If this thread is blocked in an I/O operation upon an {@link
 907      * java.nio.channels.InterruptibleChannel </code>interruptible




 795      *       Thread.stop causes it to unlock all of the monitors that it
 796      *       has locked (as a natural consequence of the unchecked
 797      *       <code>ThreadDeath</code> exception propagating up the stack).  If
 798      *       any of the objects previously protected by these monitors were in
 799      *       an inconsistent state, the damaged objects become visible to
 800      *       other threads, potentially resulting in arbitrary behavior.  Many
 801      *       uses of <code>stop</code> should be replaced by code that simply
 802      *       modifies some variable to indicate that the target thread should
 803      *       stop running.  The target thread should check this variable
 804      *       regularly, and return from its run method in an orderly fashion
 805      *       if the variable indicates that it is to stop running.  If the
 806      *       target thread waits for long periods (on a condition variable,
 807      *       for example), the <code>interrupt</code> method should be used to
 808      *       interrupt the wait.
 809      *       For more information, see
 810      *       <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 811      *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 812      */
 813     @Deprecated
 814     public final void stop() {

























































 815         SecurityManager security = System.getSecurityManager();
 816         if (security != null) {
 817             checkAccess();
 818             if (this != Thread.currentThread()) {

 819                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 820             }
 821         }
 822         // A zero status value corresponds to "NEW", it can't change to
 823         // not-NEW because we hold the lock.
 824         if (threadStatus != 0) {
 825             resume(); // Wake up thread if it was suspended; no-op otherwise
 826         }
 827 
 828         // The VM can handle all thread states
 829         stop0(new ThreadDeath());
 830     }
 831 
 832     /**
 833      * Throws {@code UnsupportedOperationException}.
 834      *
 835      * @param obj ignored
 836      *
 837      * @deprecated This method was originally designed to force a thread to stop
 838      *        and throw a given {@code Throwable} as an exception. It was
 839      *        inherently unsafe (see {@link #stop()} for details), and furthermore
 840      *        could be used to generate exceptions that the target thread was
 841      *        not prepared to handle.
 842      *        For more information, see
 843      *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 844      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 845      */
 846     @Deprecated
 847     public final synchronized void stop(Throwable obj) {
 848         throw new UnsupportedOperationException();
 849     }
 850 
 851     /**
 852      * Interrupts this thread.
 853      *
 854      * <p> Unless the current thread is interrupting itself, which is
 855      * always permitted, the {@link #checkAccess() checkAccess} method
 856      * of this thread is invoked, which may cause a {@link
 857      * SecurityException} to be thrown.
 858      *
 859      * <p> If this thread is blocked in an invocation of the {@link
 860      * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 861      * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 862      * class, or of the {@link #join()}, {@link #join(long)}, {@link
 863      * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 864      * methods of this class, then its interrupt status will be cleared and it
 865      * will receive an {@link InterruptedException}.
 866      *
 867      * <p> If this thread is blocked in an I/O operation upon an {@link
 868      * java.nio.channels.InterruptibleChannel </code>interruptible