< prev index next >

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

Print this page




 137  * @see     Runnable
 138  * @see     Runtime#exit(int)
 139  * @see     #run()
 140  * @see     #stop()
 141  * @since   1.0
 142  */
 143 public
 144 class Thread implements Runnable {
 145     /* Make sure registerNatives is the first thing <clinit> does. */
 146     private static native void registerNatives();
 147     static {
 148         registerNatives();
 149     }
 150 
 151     private volatile String name;
 152     private int priority;
 153 
 154     /* Whether or not the thread is a daemon thread. */
 155     private boolean daemon = false;
 156 



 157     /* Fields reserved for exclusive use by the JVM */
 158     private boolean stillborn = false;
 159     private long eetop;
 160 
 161     /* What will be run. */
 162     private Runnable target;
 163 
 164     /* The group of this thread */
 165     private ThreadGroup group;
 166 
 167     /* The context ClassLoader for this thread */
 168     private ClassLoader contextClassLoader;
 169 
 170     /* The inherited AccessControlContext of this thread */
 171     private AccessControlContext inheritedAccessControlContext;
 172 
 173     /* For autonumbering anonymous threads. */
 174     private static int threadInitNumber;
 175     private static synchronized int nextThreadNum() {
 176         return threadInitNumber++;


 954      * methods of this class, then its interrupt status will be cleared and it
 955      * will receive an {@link InterruptedException}.
 956      *
 957      * <p> If this thread is blocked in an I/O operation upon an {@link
 958      * java.nio.channels.InterruptibleChannel InterruptibleChannel}
 959      * then the channel will be closed, the thread's interrupt
 960      * status will be set, and the thread will receive a {@link
 961      * java.nio.channels.ClosedByInterruptException}.
 962      *
 963      * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
 964      * then the thread's interrupt status will be set and it will return
 965      * immediately from the selection operation, possibly with a non-zero
 966      * value, just as if the selector's {@link
 967      * java.nio.channels.Selector#wakeup wakeup} method were invoked.
 968      *
 969      * <p> If none of the previous conditions hold then this thread's interrupt
 970      * status will be set. </p>
 971      *
 972      * <p> Interrupting a thread that is not alive need not have any effect.
 973      *




 974      * @throws  SecurityException
 975      *          if the current thread cannot modify this thread
 976      *
 977      * @revised 6.0
 978      * @spec JSR-51
 979      */
 980     public void interrupt() {
 981         if (this != Thread.currentThread()) {
 982             checkAccess();
 983 
 984             // thread may be blocked in an I/O operation
 985             synchronized (blockerLock) {
 986                 Interruptible b = blocker;
 987                 if (b != null) {
 988                     interrupt0();  // set interrupt status

 989                     b.interrupt(this);
 990                     return;
 991                 }
 992             }
 993         }
 994 
 995         // set interrupt status
 996         interrupt0();
 997     }
 998 
 999     /**
1000      * Tests whether the current thread has been interrupted.  The
1001      * <i>interrupted status</i> of the thread is cleared by this method.  In
1002      * other words, if this method were to be called twice in succession, the
1003      * second call would return false (unless the current thread were
1004      * interrupted again, after the first call had cleared its interrupted
1005      * status and before the second call had examined it).
1006      *
1007      * <p>A thread interruption ignored because a thread was not alive
1008      * at the time of the interrupt will be reflected by this method
1009      * returning false.
1010      *
1011      * @return  {@code true} if the current thread has been interrupted;
1012      *          {@code false} otherwise.
1013      * @see #isInterrupted()
1014      * @revised 6.0
1015      */
1016     public static boolean interrupted() {
1017         return currentThread().isInterrupted(true);









1018     }
1019 
1020     /**
1021      * Tests whether this thread has been interrupted.  The <i>interrupted
1022      * status</i> of the thread is unaffected by this method.
1023      *
1024      * <p>A thread interruption ignored because a thread was not alive
1025      * at the time of the interrupt will be reflected by this method
1026      * returning false.
1027      *
1028      * @return  {@code true} if this thread has been interrupted;
1029      *          {@code false} otherwise.
1030      * @see     #interrupted()
1031      * @revised 6.0
1032      */
1033     public boolean isInterrupted() {
1034         return isInterrupted(false);
1035     }
1036 
1037     /**
1038      * Tests if some Thread has been interrupted.  The interrupted state
1039      * is reset or not based on the value of ClearInterrupted that is
1040      * passed.
1041      */
1042     @HotSpotIntrinsicCandidate
1043     private native boolean isInterrupted(boolean ClearInterrupted);
1044 
1045     /**
1046      * Tests if this thread is alive. A thread is alive if it has
1047      * been started and has not yet died.
1048      *
1049      * @return  {@code true} if this thread is alive;
1050      *          {@code false} otherwise.
1051      */
1052     public final native boolean isAlive();
1053 
1054     /**
1055      * Suspends this thread.
1056      * <p>
1057      * First, the {@code checkAccess} method of this thread is called
1058      * with no arguments. This may result in throwing a
1059      * {@code SecurityException }(in the current thread).
1060      * <p>
1061      * If the thread is alive, it is suspended and makes no further
1062      * progress unless and until it is resumed.
1063      *
1064      * @throws     SecurityException  if the current thread cannot modify
1065      *             this thread.


2063     // Hence, the fields are isolated with @Contended.
2064 
2065     /** The current seed for a ThreadLocalRandom */
2066     @jdk.internal.vm.annotation.Contended("tlr")
2067     long threadLocalRandomSeed;
2068 
2069     /** Probe hash value; nonzero if threadLocalRandomSeed initialized */
2070     @jdk.internal.vm.annotation.Contended("tlr")
2071     int threadLocalRandomProbe;
2072 
2073     /** Secondary seed isolated from public ThreadLocalRandom sequence */
2074     @jdk.internal.vm.annotation.Contended("tlr")
2075     int threadLocalRandomSecondarySeed;
2076 
2077     /* Some private helper methods */
2078     private native void setPriority0(int newPriority);
2079     private native void stop0(Object o);
2080     private native void suspend0();
2081     private native void resume0();
2082     private native void interrupt0();

2083     private native void setNativeName(String name);
2084 }


 137  * @see     Runnable
 138  * @see     Runtime#exit(int)
 139  * @see     #run()
 140  * @see     #stop()
 141  * @since   1.0
 142  */
 143 public
 144 class Thread implements Runnable {
 145     /* Make sure registerNatives is the first thing <clinit> does. */
 146     private static native void registerNatives();
 147     static {
 148         registerNatives();
 149     }
 150 
 151     private volatile String name;
 152     private int priority;
 153 
 154     /* Whether or not the thread is a daemon thread. */
 155     private boolean daemon = false;
 156 
 157     /* Interrupt state of the thread - read/written directly by JVM */
 158     private volatile boolean interrupted;
 159 
 160     /* Fields reserved for exclusive use by the JVM */
 161     private boolean stillborn = false;
 162     private long eetop;
 163 
 164     /* What will be run. */
 165     private Runnable target;
 166 
 167     /* The group of this thread */
 168     private ThreadGroup group;
 169 
 170     /* The context ClassLoader for this thread */
 171     private ClassLoader contextClassLoader;
 172 
 173     /* The inherited AccessControlContext of this thread */
 174     private AccessControlContext inheritedAccessControlContext;
 175 
 176     /* For autonumbering anonymous threads. */
 177     private static int threadInitNumber;
 178     private static synchronized int nextThreadNum() {
 179         return threadInitNumber++;


 957      * methods of this class, then its interrupt status will be cleared and it
 958      * will receive an {@link InterruptedException}.
 959      *
 960      * <p> If this thread is blocked in an I/O operation upon an {@link
 961      * java.nio.channels.InterruptibleChannel InterruptibleChannel}
 962      * then the channel will be closed, the thread's interrupt
 963      * status will be set, and the thread will receive a {@link
 964      * java.nio.channels.ClosedByInterruptException}.
 965      *
 966      * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
 967      * then the thread's interrupt status will be set and it will return
 968      * immediately from the selection operation, possibly with a non-zero
 969      * value, just as if the selector's {@link
 970      * java.nio.channels.Selector#wakeup wakeup} method were invoked.
 971      *
 972      * <p> If none of the previous conditions hold then this thread's interrupt
 973      * status will be set. </p>
 974      *
 975      * <p> Interrupting a thread that is not alive need not have any effect.
 976      *
 977      * @implNote In the JDK Reference Implementation, interruption of a thread
 978      * that is not alive still records that the interrupt request was made and
 979      * will report it via {@link #interrupted} and {@link #isInterrupted()}.
 980      *
 981      * @throws  SecurityException
 982      *          if the current thread cannot modify this thread
 983      *
 984      * @revised 6.0, 14
 985      * @spec JSR-51
 986      */
 987     public void interrupt() {
 988         if (this != Thread.currentThread()) {
 989             checkAccess();
 990 
 991             // thread may be blocked in an I/O operation
 992             synchronized (blockerLock) {
 993                 Interruptible b = blocker;
 994                 if (b != null) {
 995                     interrupted = true;
 996                     interrupt0();  // inform VM of interrupt
 997                     b.interrupt(this);
 998                     return;
 999                 }
1000             }
1001         }
1002         interrupted = true;
1003         // inform VM of interrupt
1004         interrupt0();
1005     }
1006 
1007     /**
1008      * Tests whether the current thread has been interrupted.  The
1009      * <i>interrupted status</i> of the thread is cleared by this method.  In
1010      * other words, if this method were to be called twice in succession, the
1011      * second call would return false (unless the current thread were
1012      * interrupted again, after the first call had cleared its interrupted
1013      * status and before the second call had examined it).
1014      *




1015      * @return  {@code true} if the current thread has been interrupted;
1016      *          {@code false} otherwise.
1017      * @see #isInterrupted()
1018      * @revised 6.0, 14
1019      */
1020     public static boolean interrupted() {
1021         Thread t = currentThread();
1022         boolean interrupted = t.interrupted;
1023         // We may have been interrupted the moment after we read the field,
1024         // so only clear the field if we saw that it was set and will return
1025         // true; otherwise we could lose an interrupt.
1026         if (interrupted) {
1027             t.interrupted = false;
1028             clearInterruptEvent();
1029         }
1030         return interrupted;
1031     }
1032 
1033     /**
1034      * Tests whether this thread has been interrupted.  The <i>interrupted
1035      * status</i> of the thread is unaffected by this method.
1036      *




1037      * @return  {@code true} if this thread has been interrupted;
1038      *          {@code false} otherwise.
1039      * @see     #interrupted()
1040      * @revised 6.0, 14
1041      */
1042     public boolean isInterrupted() {
1043         return interrupted;
1044     }
1045 
1046     /**








1047      * Tests if this thread is alive. A thread is alive if it has
1048      * been started and has not yet died.
1049      *
1050      * @return  {@code true} if this thread is alive;
1051      *          {@code false} otherwise.
1052      */
1053     public final native boolean isAlive();
1054 
1055     /**
1056      * Suspends this thread.
1057      * <p>
1058      * First, the {@code checkAccess} method of this thread is called
1059      * with no arguments. This may result in throwing a
1060      * {@code SecurityException }(in the current thread).
1061      * <p>
1062      * If the thread is alive, it is suspended and makes no further
1063      * progress unless and until it is resumed.
1064      *
1065      * @throws     SecurityException  if the current thread cannot modify
1066      *             this thread.


2064     // Hence, the fields are isolated with @Contended.
2065 
2066     /** The current seed for a ThreadLocalRandom */
2067     @jdk.internal.vm.annotation.Contended("tlr")
2068     long threadLocalRandomSeed;
2069 
2070     /** Probe hash value; nonzero if threadLocalRandomSeed initialized */
2071     @jdk.internal.vm.annotation.Contended("tlr")
2072     int threadLocalRandomProbe;
2073 
2074     /** Secondary seed isolated from public ThreadLocalRandom sequence */
2075     @jdk.internal.vm.annotation.Contended("tlr")
2076     int threadLocalRandomSecondarySeed;
2077 
2078     /* Some private helper methods */
2079     private native void setPriority0(int newPriority);
2080     private native void stop0(Object o);
2081     private native void suspend0();
2082     private native void resume0();
2083     private native void interrupt0();
2084     private static native void clearInterruptEvent();
2085     private native void setNativeName(String name);
2086 }
< prev index next >