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

Print this page




 237         synchronized (blockerLock) {
 238             blocker = b;
 239         }
 240     }
 241 
 242     /**
 243      * The minimum priority that a thread can have.
 244      */
 245     public final static int MIN_PRIORITY = 1;
 246 
 247    /**
 248      * The default priority that is assigned to a thread.
 249      */
 250     public final static int NORM_PRIORITY = 5;
 251 
 252     /**
 253      * The maximum priority that a thread can have.
 254      */
 255     public final static int MAX_PRIORITY = 10;
 256 
 257     /* If stop was called before start */
 258     private boolean stopBeforeStart;
 259 
 260     /* Remembered Throwable from stop before start */
 261     private Throwable throwableFromStop;
 262 
 263     /**
 264      * Returns a reference to the currently executing thread object.
 265      *
 266      * @return  the currently executing thread.
 267      */
 268     public static native Thread currentThread();
 269 
 270     /**
 271      * A hint to the scheduler that the current thread is willing to yield
 272      * its current use of a processor. The scheduler is free to ignore this
 273      * hint.
 274      *
 275      * <p> Yield is a heuristic attempt to improve relative progression
 276      * between threads that would otherwise over-utilise a CPU. Its use
 277      * should be combined with detailed profiling and benchmarking to
 278      * ensure that it actually has the desired effect.
 279      *
 280      * <p> It is rarely appropriate to use this method. It may be useful
 281      * for debugging or testing purposes, where it may help to reproduce
 282      * bugs due to race conditions. It may also be useful when designing


 689 
 690         /* Notify the group that this thread is about to be started
 691          * so that it can be added to the group's list of threads
 692          * and the group's unstarted count can be decremented. */
 693         group.add(this);
 694 
 695         boolean started = false;
 696         try {
 697             start0();
 698             started = true;
 699         } finally {
 700             try {
 701                 if (!started) {
 702                     group.threadStartFailed(this);
 703                 }
 704             } catch (Throwable ignore) {
 705                 /* do nothing. If start0 threw a Throwable then
 706                   it will be passed up the call stack */
 707             }
 708         }
 709 
 710         if (stopBeforeStart) {
 711             stop0(throwableFromStop);
 712         }
 713     }
 714 
 715     private native void start0();
 716 
 717     /**
 718      * If this thread was constructed using a separate
 719      * <code>Runnable</code> run object, then that
 720      * <code>Runnable</code> object's <code>run</code> method is called;
 721      * otherwise, this method does nothing and returns.
 722      * <p>
 723      * Subclasses of <code>Thread</code> should override this method.
 724      *
 725      * @see     #start()
 726      * @see     #stop()
 727      * @see     #Thread(ThreadGroup, Runnable, String)
 728      */
 729     @Override
 730     public void run() {
 731         if (target != null) {
 732             target.run();
 733         }


 803      *       Thread.stop causes it to unlock all of the monitors that it
 804      *       has locked (as a natural consequence of the unchecked
 805      *       <code>ThreadDeath</code> exception propagating up the stack).  If
 806      *       any of the objects previously protected by these monitors were in
 807      *       an inconsistent state, the damaged objects become visible to
 808      *       other threads, potentially resulting in arbitrary behavior.  Many
 809      *       uses of <code>stop</code> should be replaced by code that simply
 810      *       modifies some variable to indicate that the target thread should
 811      *       stop running.  The target thread should check this variable
 812      *       regularly, and return from its run method in an orderly fashion
 813      *       if the variable indicates that it is to stop running.  If the
 814      *       target thread waits for long periods (on a condition variable,
 815      *       for example), the <code>interrupt</code> method should be used to
 816      *       interrupt the wait.
 817      *       For more information, see
 818      *       <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 819      *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 820      */
 821     @Deprecated
 822     public final void stop() {
 823         // If the thread is already dead, return.
 824         // A zero status value corresponds to "NEW".
 825         if ((threadStatus != 0) && !isAlive()) {
 826             return;
 827         }
 828         stop1(new ThreadDeath());
 829     }
 830 
 831     /**
 832      * Forces the thread to stop executing.
 833      * <p>
 834      * If there is a security manager installed, the <code>checkAccess</code>
 835      * method of this thread is called, which may result in a
 836      * <code>SecurityException</code> being raised (in the current thread).
 837      * <p>
 838      * If this thread is different from the current thread (that is, the current
 839      * thread is trying to stop a thread other than itself) or
 840      * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
 841      * security manager's <code>checkPermission</code> method (with the
 842      * <code>RuntimePermission("stopThread")</code> argument) is called in
 843      * addition.
 844      * Again, this may result in throwing a
 845      * <code>SecurityException</code> (in the current thread).
 846      * <p>
 847      * If the argument <code>obj</code> is null, a
 848      * <code>NullPointerException</code> is thrown (in the current thread).
 849      * <p>


 862      * @throws     NullPointerException if obj is <tt>null</tt>.
 863      * @see        #interrupt()
 864      * @see        #checkAccess()
 865      * @see        #run()
 866      * @see        #start()
 867      * @see        #stop()
 868      * @see        SecurityManager#checkAccess(Thread)
 869      * @see        SecurityManager#checkPermission
 870      * @deprecated This method is inherently unsafe.  See {@link #stop()}
 871      *        for details.  An additional danger of this
 872      *        method is that it may be used to generate exceptions that the
 873      *        target thread is unprepared to handle (including checked
 874      *        exceptions that the thread could not possibly throw, were it
 875      *        not for this method).
 876      *        For more information, see
 877      *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 878      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 879      */
 880     @Deprecated
 881     public final synchronized void stop(Throwable obj) {
 882         stop1(obj);
 883     }
 884 
 885     /**
 886      * Common impl for stop() and stop(Throwable).
 887      */
 888     private final synchronized void stop1(Throwable th) {
 889         SecurityManager security = System.getSecurityManager();
 890         if (security != null) {
 891             checkAccess();
 892             if ((this != Thread.currentThread()) ||
 893                 (!(th instanceof ThreadDeath))) {
 894                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 895             }
 896         }
 897         // A zero status value corresponds to "NEW"

 898         if (threadStatus != 0) {
 899             resume(); // Wake up thread if it was suspended; no-op otherwise
 900             stop0(th);
 901         } else {
 902 
 903             // Must do the null arg check that the VM would do with stop0
 904             if (th == null) {
 905                 throw new NullPointerException();
 906             }
 907 
 908             // Remember this stop attempt for if/when start is used
 909             stopBeforeStart = true;
 910             throwableFromStop = th;
 911         }
 912     }
 913 
 914     /**
 915      * Interrupts this thread.
 916      *
 917      * <p> Unless the current thread is interrupting itself, which is
 918      * always permitted, the {@link #checkAccess() checkAccess} method
 919      * of this thread is invoked, which may cause a {@link
 920      * SecurityException} to be thrown.
 921      *
 922      * <p> If this thread is blocked in an invocation of the {@link
 923      * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
 924      * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
 925      * class, or of the {@link #join()}, {@link #join(long)}, {@link
 926      * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
 927      * methods of this class, then its interrupt status will be cleared and it
 928      * will receive an {@link InterruptedException}.
 929      *
 930      * <p> If this thread is blocked in an I/O operation upon an {@link
 931      * java.nio.channels.InterruptibleChannel </code>interruptible
 932      * channel<code>} then the channel will be closed, the thread's interrupt




 237         synchronized (blockerLock) {
 238             blocker = b;
 239         }
 240     }
 241 
 242     /**
 243      * The minimum priority that a thread can have.
 244      */
 245     public final static int MIN_PRIORITY = 1;
 246 
 247    /**
 248      * The default priority that is assigned to a thread.
 249      */
 250     public final static int NORM_PRIORITY = 5;
 251 
 252     /**
 253      * The maximum priority that a thread can have.
 254      */
 255     public final static int MAX_PRIORITY = 10;
 256 






 257     /**
 258      * Returns a reference to the currently executing thread object.
 259      *
 260      * @return  the currently executing thread.
 261      */
 262     public static native Thread currentThread();
 263 
 264     /**
 265      * A hint to the scheduler that the current thread is willing to yield
 266      * its current use of a processor. The scheduler is free to ignore this
 267      * hint.
 268      *
 269      * <p> Yield is a heuristic attempt to improve relative progression
 270      * between threads that would otherwise over-utilise a CPU. Its use
 271      * should be combined with detailed profiling and benchmarking to
 272      * ensure that it actually has the desired effect.
 273      *
 274      * <p> It is rarely appropriate to use this method. It may be useful
 275      * for debugging or testing purposes, where it may help to reproduce
 276      * bugs due to race conditions. It may also be useful when designing


 683 
 684         /* Notify the group that this thread is about to be started
 685          * so that it can be added to the group's list of threads
 686          * and the group's unstarted count can be decremented. */
 687         group.add(this);
 688 
 689         boolean started = false;
 690         try {
 691             start0();
 692             started = true;
 693         } finally {
 694             try {
 695                 if (!started) {
 696                     group.threadStartFailed(this);
 697                 }
 698             } catch (Throwable ignore) {
 699                 /* do nothing. If start0 threw a Throwable then
 700                   it will be passed up the call stack */
 701             }
 702         }



 703     }

 704 
 705     private native void start0();
 706 
 707     /**
 708      * If this thread was constructed using a separate
 709      * <code>Runnable</code> run object, then that
 710      * <code>Runnable</code> object's <code>run</code> method is called;
 711      * otherwise, this method does nothing and returns.
 712      * <p>
 713      * Subclasses of <code>Thread</code> should override this method.
 714      *
 715      * @see     #start()
 716      * @see     #stop()
 717      * @see     #Thread(ThreadGroup, Runnable, String)
 718      */
 719     @Override
 720     public void run() {
 721         if (target != null) {
 722             target.run();
 723         }


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



 814     }


 815 
 816     /**
 817      * Forces the thread to stop executing.
 818      * <p>
 819      * If there is a security manager installed, the <code>checkAccess</code>
 820      * method of this thread is called, which may result in a
 821      * <code>SecurityException</code> being raised (in the current thread).
 822      * <p>
 823      * If this thread is different from the current thread (that is, the current
 824      * thread is trying to stop a thread other than itself) or
 825      * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
 826      * security manager's <code>checkPermission</code> method (with the
 827      * <code>RuntimePermission("stopThread")</code> argument) is called in
 828      * addition.
 829      * Again, this may result in throwing a
 830      * <code>SecurityException</code> (in the current thread).
 831      * <p>
 832      * If the argument <code>obj</code> is null, a
 833      * <code>NullPointerException</code> is thrown (in the current thread).
 834      * <p>


 847      * @throws     NullPointerException if obj is <tt>null</tt>.
 848      * @see        #interrupt()
 849      * @see        #checkAccess()
 850      * @see        #run()
 851      * @see        #start()
 852      * @see        #stop()
 853      * @see        SecurityManager#checkAccess(Thread)
 854      * @see        SecurityManager#checkPermission
 855      * @deprecated This method is inherently unsafe.  See {@link #stop()}
 856      *        for details.  An additional danger of this
 857      *        method is that it may be used to generate exceptions that the
 858      *        target thread is unprepared to handle (including checked
 859      *        exceptions that the thread could not possibly throw, were it
 860      *        not for this method).
 861      *        For more information, see
 862      *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
 863      *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
 864      */
 865     @Deprecated
 866     public final synchronized void stop(Throwable obj) {







 867         SecurityManager security = System.getSecurityManager();
 868         if (security != null) {
 869             checkAccess();
 870             if ((this != Thread.currentThread()) ||
 871                 (!(obj instanceof ThreadDeath))) {
 872                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
 873             }
 874         }
 875         // A zero status value corresponds to "NEW", it can't change to
 876         // not-NEW because we hold the lock.
 877         if (threadStatus != 0) {
 878             resume(); // Wake up thread if it was suspended; no-op otherwise






 879         }
 880         
 881         // The VM can handle all thread states
 882         stop0(obj);

 883     }

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